Categories
Python Answers

How to fix Python Django admin site not have styles or CSS loading?

Sometimes, we want to fix Python Django admin site not have styles or CSS loading.

In this article, we’ll look at how to fix Python Django admin site not have styles or CSS loading.

How to fix Python Django admin site not have styles or CSS loading?

To fix Python Django admin site not have styles or CSS loading, we run the collectstatic command.

For instance, we run

python manage.py collectstatic

in our app project after the STATIC_ROOT and STATIC_URL settings are set.

Conclusion

To fix Python Django admin site not have styles or CSS loading, we run the collectstatic command.

Categories
Python Answers

How to expire Python Django session in 5 minutes?

Sometimes, we want to expire Python Django session in 5 minutes.

In this article, we’ll look at how to expire Python Django session in 5 minutes.

How to expire Python Django session in 5 minutes?

To expire Python Django session in 5 minutes, we can set the SESSION_COOKIE_AGE setting in settings.py.

For instance, we write

SESSION_COOKIE_AGE = 5*60

in settings.py to set sessions to expire in 5 minutes.

Conclusion

To expire Python Django session in 5 minutes, we can set the SESSION_COOKIE_AGE setting in settings.py.

Categories
Python Answers

How to parse request.body from POST in Python Django?

Sometimes, we want to parse request.body from POST in Python Django.

In this article, we’ll look at how to parse request.body from POST in Python Django.

How to parse request.body from POST in Python Django?

To parse request.body from POST in Python Django, we can call decode to decode the request.body binary string into a JSON string.

Then we call json.loads with the returned string to convert it to a dict.

For instance, we write

body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
content = body['content']

We call request.body.decode with the string encoding to decode to to decode the request.body binary string.

Then we call json.loads with the body_unicode JSON string to return a dict from the JSON string.

Conclusion

To parse request.body from POST in Python Django, we can call decode to decode the request.body binary string into a JSON string.

Then we call json.loads with the returned string to convert it to a dict.

Categories
Python Answers

How to disable Python Django’s CSRF validation?

Sometimes, we want to disable Python Django’s CSRF validation.

In this article, we’ll look at how to disable Python Django’s CSRF validation.

How to disable Python Django’s CSRF validation?

To disable Python Django’s CSRF validation, we can use the csrf_exempt decorator on a view.

For instance, we write

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')

to create the my_view view.

We use the csrf_exempt decorator to make Django skip CSRF validation when making a request to this view.

Conclusion

To disable Python Django’s CSRF validation, we can use the csrf_exempt decorator on a view.

Categories
Python Answers

How to show a PDF file in a Python Django view?

Sometimes, we want to show a PDF file in a Python Django view.

In this article, we’ll look at how to show a PDF file in a Python Django view.

How to show a PDF file in a Python Django view?

To show a PDF file in a Python Django view, we can create a FileResponse and return it.

For instance, we write

from django.http import FileResponse, Http404

def pdf_view(request):
    try:
        return FileResponse(open('foobar.pdf', 'rb'), content_type='application/pdf')
    except FileNotFoundError:
        raise Http404()

to create the pdf_view view that returns the FileResponse object that we create by opening the foobar.pdf PDF file.

We set the content_type argument to 'application/pdf' to make the client know that the file is a PDF.

If the file isn’t found, then we return a 404 response with Http404.

Conclusion

To show a PDF file in a Python Django view, we can create a FileResponse and return it.