I'm somehow new to Django, and I don't know how to resolve what seems to be a simple bug. A lot of peoples asked more or less the same question here but any to their fixes worked for me.
So I have 2 apps, one that will work as the main menu for other apps:
Main Menu urls.py :
re_path(r'^elec/boq/', include('a0101_boq_elec_main.urls', namespace="SAMM")),
Then in the urls.py of that app I have this:
app_name='BoqElecMM'
urlpatterns = [
path('', views.boq_index.as_view(), name='boqIndex'),
path('search/', views.SearchResultView.as_view(), name='searchResult'),
path('<int:boq_project_id>', views.BoqProjectInfo.as_view(), name='BPInfo'),
]
But when I'm trying to use this in my template: <a href="{% url 'BoqElecMM:BPInfo' %}"> , I'm getting this Django error :
NoReverseMatch at /elec/boq/
Reverse for 'BPInfo' not found. 'BPInfo' is not a valid view function or pattern name.
Could some of you tell me please, what I'm doing wrong here?
Thank you in advance.
You need to pass a value for the boq_project_id parameter, so for example
{% url 'BoqElecMM:BPInfo' boq_project_id=42 %}
Of course the value you pass to the boq_project_id is normally the id of the project object. So for example, if in your template you have a project object, you can write:
{% url 'BoqElecMM:BPInfo' boq_project_id=project.pk %}
if you have an namespace=… as well, then this should also be prefixed, so with the data in your view, it is:
{% url 'SAMM:BoqElecMM:BPInfo' boq_project_id=project.pk %}
Related
I have a Django project called reports with apps report_1, report_2 etc.
For certain reasons, I want to treat the project as an app, so I have added reports to INSTALLED_APPS alongside report_1 and report_2 and also created views.py file and templates folder in the main project folder (where settings.py sit).
In the urls.py I have added app_name = reports.
However, calling the url from within template will work only if I skip the app name: so {% url 'reports:index' %} will throw
'reports' is not a registered namespace'
error, but {% url 'index' %} will work.
Why is that so? I thought Django traverses all apps listed in INSTALLED_APPS, looks for app_name (which is provided) and matches it with URL name.
app_name does not work in the root url config. See ticket 28413 and this discussion on the django-developers mailing list.
If you really want to include some patterns under the report namespace, you can include a 2-tuple containing a list of url patterns and a namespace.
reports_patterns = ([
path('myurl', views.my_view, name='myview'),
], 'reports')
urlpatterns = [
...
path('', include(reports_patterns)
...
]
You could then use {% url 'reports:myview' %} in the template.
See the docs on URL namespaces and included URLconfs for more info
you can add :
app_name = 'polls'
in above urls.py on which app your are use :
urlpatterns=[
....
]
and then every where you need load template use:
{% url 'appname:index.html' %}
I have the following url patterns defined in the urls.py
urlpatterns = [
path('', views.index, name='index'),
path('start', views.start, name='start'),
path('<slug:id>' , views.person, name="person"),
path('family/<slug:id>', views.family, name="family")
]
And the first 3 works just fine, for example if I call this link:
<a href="{% url 'pops:person' id=my_id %}">
Everything works fine and I go to the persons page, The other link such as start and index also works fine.
But somehow if I do this link:
<a href="{% url 'pops:family' id=my_id %}">
Everything fails and throws a reverse not found error:
Reverse for 'index' with keyword arguments '{'id': 'BLAHBLAH'}' not found.
The problem is, I'm not even going for the index, Why is it reversing for index when it should be reversing for family? As a result, it's not even hitting the function defined in the views at all.
I have a urls.py file which contains multiple urls with same named paramaters and regex but different names. When I call a view function from the template using {% url 'name' param %} it calls the function which comes first in the urls.py file in disregard to the name. Here is the content of urls.py:
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_password',name='reset_password'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_securityquestions',name='reset_securityquestions'),
I am trying to call reset_securityquestions from the template using:
"{% url 'reset_securityquestions' account.uuid user.uuid %}">
but it calls reset_password instead.
If I change the order of urls in urls.py to this:
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$', 'reset_securityquestions',
name='reset_securityquestions'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$', 'reset_password', name='reset_password'),
and call reset_password using:
{% url 'reset_password' account.uuid user.uuid %}
it calls the reset_security_questions function. Where am I going wrong?
Django always take firsts matching url pattern , so rewrite the urls as :
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'^reset-password/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_password',name='reset_password'),
url(r'^security-question/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_securityquestions',name='reset_securityquestions'),
I don't understand how you are expecting Django to know which URL to use if they both have exactly the same url pattern. When the request comes in, all Django has to go on is the URL itself, and it will always try and match those in order. With your switched patterns, you'll find that the reset_password view now doesn't work.
You need to give Django some way of distinguishing between them. Usually some literal text, either as a prefix or a suffix, would be used, for example:
r'reset/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$'
r'security/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$'
I'd like users to be be able to reset their passwords via a link on my site. I'm trying to use the built in django password reset form, but having a little difficulty. I have the following included in my urls.py
urlpatterns = patterns('',
url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', name='admin_password_reset'),
url(r'^admin/password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
url(r'^admin/', include(admin.site.urls)),
I access the link in the following way:
<h3>Forgot your password?</h3>
I've also tried including it like this:
{% url django.contrib.auth.views.password_reset_confirm uid, token %}
{% url django.contrib.auth.views.password_reset_confirm uidb36=uid, token=token %}
I get the following error:
NoReverseMatch at /account/
Reverse for 'password_reset' with arguments '()' and keyword arguments '{}' not found.
I've tried a bunch of different tings without any luck. Do I need to pass in the uid/token? If so, how do I define them?
Thank you for your help
Your url is named admin_password_reset
<h3>Forgot your password?</h3>
This sounds very similar to an issue I had. You might check my solution: Django {% url %} reverse not working if you are using {% load url from future %}.
Good luck!
I just started learning python and django and I have a question.
I got the assignment to turn function views into class based views. But my links wont work now.
these are from urls.py:
url(r'^$', ContactIndex.as_view()),
url(r'^add$', ContactAdd.as_view()),
url(r'^([0-9]+)/update$', ContactUpdate.as_view()),
url(r'^([0-9]+)/view$', ContactView.as_view()),
This is my link :
{% url rtr_contact.views.ContactView contact.id %}
but this doesnt work it says:
Caught NoReverseMatch while rendering: Reverse for 'rtr_contact.views.ContactView' with arguments '(20L,)' and keyword arguments '{}' not found.
To make url reversing easy, I recommend that you always name your url patterns.
url(r'^$', ContactIndex.as_view(), name="contact_index"),
url(r'^add$', ContactAdd.as_view(), name="contact_add"),
url(r'^([0-9]+)/update$', ContactUpdate.as_view(), name="contact_update"),
url(r'^([0-9]+)/view$', ContactView.as_view(), name="contact_view"),
Then in the template:
{% url contact_view contact.id %}