Closed agieocean closed 4 years ago
What's in your views.index
function? Also can you paste the full stack trace?
Aside from your error, creating a static version of the login form (or any form) is a bit pointless, it'll render the form but it won't do anything obviously.
For the views.index function:
def index(request):
if ageCheck(request) == False:
return redirect("/ageVerification")
if isAllowed(request) != True:
return redirect("/register")
if request.user.is_authenticated:
subscriptions = [s.model for s in Subscription.objects.filter(user=request.user, subscribed=True)]
posts = json.loads(serializers.serialize("json", Post.objects.filter(model__in=subscriptions, available__lt=datetime.datetime.now()).order_by("-available")))
context = {"groups": [group.name for group in request.user.groups.all()], "posts": posts}
return render(request, "user/index.html", context)
else:
context = {}
return render(request, "user/index.html", context)
Looking at this do I need to create and pass a request object, I think I naively assumed that would be handled automatically but let me know when you have a chance ^_^ Here's the full output:
python manage.py distill-local site-export
You have requested to create a static version of
this site into the output path directory:
Source static path: .
Distill output path: [redacted]\site-export
Distill output directory exists, clean up?
This will delete and recreate all files in the output dir
Type 'yes' to continue, or 'no' to cancel: yes
Recreating output directory...
Generating static site into directory: [redacted]\site-export
Loading site URLs
CommandError: Failed to render view "/": 'WSGIRequest' object has no attribute 'session'
You do not need to pass in a custom requests object, this is handled for you. However, this view, I suspect, when run via distill
won't do anything. It'll return a redirect, and probably throw an error for not returning a 200 status code. You can't distill
dynamic actions (decision based redirects, forms, etc.) only static content. For example this will work fine:
def index(request):
return render(request, "index.html")
But this will not:
def index(request):
if not request.user.is_authenticated:
return redirect("/signup")
return render(request, "index.html")
During building the static site it will always trigger the redirect and result in trying to render a redirect and throwing an error. To make a flat, static site you can only render static views which return HTML and 200 HTTP status code, and it is advisable the views are consistent so always return the same thing when they are called. Obviously a static site can't support any database or session features at all as no code executes anywhere when looking at a static HTML page.
As for your session
error, looks like it could be related to the order of your middleware, does this site work normally without distill
under ./manage.py runserver
? I'm guessing your ageCheck
function tries to set some sort of session variable?
Fantastic thank you so much for the details! This site does normally run fine with ./manage.py runserver and ageCheck is just this:
def ageCheck(request):
try:
if request.session['verify'] != "true":
return False
except KeyError:
return False
After a quick search, your error is caused because internally distill
uses Django's RequestFactory()
to create a request to render the views and generate the HTML to save to disk. The request
object returned by RequestFactory()
doesn't support sessions by design:
https://docs.djangoproject.com/en/3.1/topics/testing/advanced/#django.test.RequestFactory https://code.djangoproject.com/ticket/15736
This hasn't come up previously as I don't think anyone has attempted to set sessions in a static site before. As said above, the sessions will do nothing in a static site and make no logical sense, however I can see how it may occur that someone uses sessions in an existing site which then gets wrapped in with distill
and the error is not obvious.
I've pushed a new release of distill
, version 2.1, this includes a dummy session interface which will raise a polite error rather than a hard exception. This should let you know you're doing something weird, but not stop the static site from being generated, so you'll see something like this instead now:
RuntimeWarning: request.session.__setitem__(('test', 'test'), {}) called. This is a dummy interface. The request.session feature of Django is not supported by distill. This request will do nothing and has no effect when rendering a static site. If this request is a requirement for the function of your site when not being rendered you can ignore this warning.
Upgrade to version 2.1 and see if that works better for you.
Obviously, your site has other issues (user interactivity, session based data storage, user messaging etc. based on your URL paths) so you may want to reconsider if making a static site from your project is appropriate in the first place, unless you're making some static archive of an old site. If you do proceed with attempting to make a static version of your dynamic site note your message/<str:username>
URL will require its distill_func
to return an iterable of username strings to work as the URL has required parameters.
As you've not replied for a week I'll assume this has resolved your issue and close it. Feel free to open another issue if you encounter any more problems.
Running
python manage.py distill-local site-export
produces the responseCommandError: Failed to render view "/": 'WSGIRequest' object has no attribute 'session'
URLs.py config: