How can I list urlpatterns (endpoints) on Django?

If you want a list of all the urls in your project, first you need to install django-extensions You can simply install using command. pip install django-extensions For more information related to package goto django-extensions After that, add django_extensions in INSTALLED_APPS in your settings.py file like this: INSTALLED_APPS = ( … ‘django_extensions’, … ) urls.py … Read more

Using request.user with Django ModelForm

You just need to exclude it from the form, then set it in the view. class AnimalForm(ModelForm): class Meta: model = Animal exclude = (‘publisher’,) … and in the view: form = AnimalForm(request.POST) if form.is_valid(): animal = form.save(commit=False) animal.publisher = request.user animal.save() (Note also that the first else clause – the lines immediately following the … Read more

ValueError: Missing staticfiles manifest entry for ‘favicon.ico’

Try running: python manage.py collectstatic Does the test work now? If so, this might be the configuration causing a problem: STATICFILES_STORAGE = ‘whitenoise.django.GzipManifestStaticFilesStorage’ as of whitenoise v4 this will fail and you should use: STATICFILES_STORAGE = ‘whitenoise.storage.CompressedManifestStaticFilesStorage’ Related: https://stackoverflow.com/a/32347324/2596187 Check out the Django documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict

Can I call a view from within another view?

Sure, as long as when it’s all said and done your view returns an HttpResponse object. The following is completely valid: def view1(request): # do some stuff here return HttpResponse(“some html here”) def view2(request): return view1(request) If you don’t want to return the HttpResponse from the first view then just store it into some variable … Read more

Django: CSRF token missing or incorrect

You need to pass RequestContext in render_to_response for csrf_token For this : (views.py) from django.template import RequestContext … return render_to_response(‘fileupload/upload.html’, {‘form’: c[‘UploadFileForm’]}, RequestContext(request)) # Added RequestContext This passes the token for csrf to the template.

Provide extra context to all views

You could use the template context processor: myapp/context_processors.py: from django.contrib.auth.models import User from myapp.models import Project def users_and_projects(request): return {‘all_users’: User.objects.all(), ‘all_projects’: Project.objects.all()} And then add this processor to the TEMPLATE_CONTEXT_PROCESSORS setting for Django version < 1.8: TEMPLATE_CONTEXT_PROCESSORS = ( … ‘myapp.context_processors.users_and_projects’, ) And for Django version >= 1.8 add it to the context_processors list … Read more

How to set up custom middleware in Django

First: The path structure If you don’t have it you need to create the middleware folder within your app following the structure: yourproject/yourapp/middleware The folder middleware should be placed in the same folder as settings.py, urls, templates… Important: Don’t forget to create the init.py empty file inside the middleware folder so your app recognizes this … Read more