Django, name parameter in urlpatterns

2024/11/19 21:41:57

I'm following a tutorial where my urlpatterns are:

urlpatterns = patterns('',url(r'^passwords/$', PasswordListView.as_view(), name='passwords_api_root'),url(r'^passwords/(?P<id>[0-9]+)$', PasswordInstanceView.as_view(), name='passwords_api_instance'),...other urls here...,
)

The PasswordListView and PasswordInstanceView are supposed to be class based views. I could not figure out the meaning of the name parameter. Is it a default parameter passed to the view?

Answer

No. It is just that django gives you the option to name your views in case you need to refer to them from your code, or your templates. This is useful and good practice because you avoid hardcoding urls on your code or inside your templates. Even if you change the actual url, you don't have to change anything else, since you will refer to them by name.

e.x with views:

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse #this is deprecated in django 2.0+
from django.urls import reverse #use this for django 2.0+def myview(request):passwords_url = reverse('passwords_api_root')  # this returns the string `/passwords/`return HttpResponseRedirect(passwords_url)

More here.

e.x. in templates

<p>Please go <a href="{% url 'passwords_api_root' %}">here</a></p>

More here.

https://en.xdnf.cn/q/26392.html

Related Q&A

The most Pythonic way of checking if a value in a dictionary is defined/has zero length

Say I have a dictionary, and I want to check if a key is mapped to a nonempty value. One way of doing this would be the len function:mydict = {"key" : "value", "emptykey"…

What does the --pre option in pip signify?

I saw on this page that pip install neo4j-doc-manager --pre was used. What does the --pre flag mean?

Tracing and Returning a Path in Depth First Search

So I have a problem that I want to use depth first search to solve, returning the first path that DFS finds. Here is my (incomplete) DFS function:start = problem.getStartState()stack = Stack()visited =…

Pandas OHLC aggregation on OHLC data

I understand that OHLC re-sampling of time series data in Pandas, using one column of data, will work perfectly, for example on the following dataframe:>>df ctime openbid 1443654000 1.1170…

Python plotting libraries [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

python elasticsearch client set mappings during create index

I can set mappings of index being created in curl command like this:{ "mappings":{ "logs_june":{ "_timestamp":{ "enabled":"true"},"properties&…

Get Primary Key after Saving a ModelForm in Django

How do I get the primary key after saving a ModelForm? After the form has been validated and saved, I would like to redirect the user to the contact_details view which requires the primary key of the …

f.write vs print f

There are at least two ways to write to a file in python:f = open(file, w) f.write(string)orf = open(file, w) print >> f, string # in python 2 print(string, file=f) # in python 3Is there a d…

How can I send a message to someone with my telegram bot using their Username

I am using the telepot python library, I know that you can send a message when you have someones UserID(Which is a number). I wanna know if it is possible to send a message to someone without having th…

Convert a str to path type?

I am trying to interface with some existing code that saves a configuration, and expects a file path that is of type path.path. The code is expecting that the file path is returned from a pygtk browse…