Using {% url ??? %} in django templates

The selected answer is out of date and no others worked for me (Django 1.6 and [apparantly] no registered namespace.) For Django 1.5 and later (from the docs) Warning Don’t forget to put quotes around the function path or pattern name! With a named URL you could do: (r’^login/’, login_view, name=”login”), … <a href=”{% url … Read more

How to get the current url name using Django?

I don’t know how long this feature has been part of Django but as the following article shows, it can be achieved as follows in the view: from django.core.urlresolvers import resolve current_url = resolve(request.path_info).url_name If you need that in every template, writing a template request can be appropriate. Edit: APPLYING NEW DJANGO UPDATE Following the … Read more

ImportError: cannot import name ‘url’ from ‘django.conf.urls’ after upgrading to Django 4.0

django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+. The easiest fix is to replace url() with re_path(). re_path uses regexes like url, so you only have to update the import and replace url with re_path. from django.urls import include, re_path from myapp.views import home urlpatterns = [ re_path(r’^$’, home, name=”home”), re_path(r’^myapp/’, … 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

Django URL Redirect

You can try the Class Based View called RedirectView from django.views.generic.base import RedirectView urlpatterns = patterns(”, url(r’^$’, ‘macmonster.views.home’), #url(r’^macmon_home$’, ‘macmonster.views.home’), url(r’^macmon_output/$’, ‘macmonster.views.output’), url(r’^macmon_about/$’, ‘macmonster.views.about’), url(r’^.*$’, RedirectView.as_view(url=”<url_to_home_view>”, permanent=False), name=”index”) ) Notice how as url in the <url_to_home_view> you need to actually specify the url. permanent=False will return HTTP 302, while permanent=True will return HTTP 301. Alternatively … Read more

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)