Difference between static STATIC_URL and STATIC_ROOT on Django

STATIC_ROOT The absolute path to the directory where ./manage.py collectstatic will collect static files for deployment. Example: STATIC_ROOT=”/var/www/example.com/static/” now the command ./manage.py collectstatic will copy all the static files(ie in static folder in your apps, static files in all paths) to the directory /var/www/example.com/static/. now you only need to serve this directory on apache or … Read more

Django – after login, redirect user to his custom page –> mysite.com/username

A simpler approach relies on redirection from the page LOGIN_REDIRECT_URL. The key thing to realize is that the user information is automatically included in the request. Suppose: LOGIN_REDIRECT_URL = ‘/profiles/home’ and you have configured a urlpattern: (r’^profiles/home’, home), Then, all you need to write for the view home() is: from django.http import HttpResponseRedirect from django.urls … Read more

Django optional url parameters

There are several approaches. One is to use a non-capturing group in the regex: (?:/(?P<title>[a-zA-Z]+)/)? Making a Regex Django URL Token Optional Another, easier to follow way is to have multiple rules that matches your needs, all pointing to the same view. urlpatterns = patterns(”, url(r’^project_config/$’, views.foo), url(r’^project_config/(?P<product>\w+)/$’, views.foo), url(r’^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$’, views.foo), ) Keep in mind … Read more

Django URLs TypeError: view must be a callable or a list/tuple in the case of include()

Django 1.10 no longer allows you to specify views as a string (e.g. ‘myapp.views.home’) in your URL patterns. The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don’t have names, then now is a good time … Read more

What is a NoReverseMatch error, and how do I fix it?

The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you’ve provided in any of your installed app’s urls. The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied. To start debugging it, you need to start … Read more