I have a problem calling an url, i want to know whats the way to pass a value with 2 words like an argument or if my question is not possible
i have this in my url.py:
url(r'^tiendas/([\w-]+)/$', MostrarTiendas.as_view(), name='mostrartiendas'),
i want to pass a value called name_tienda but this field sometimes have 2 word and django return this error:
Page not found (404)
Request Method: GET
http://localhost:8000/tiendas/Tienda%20Amiga
thank for all
Change your url regex to this:
url(r'^tiendas/(?P<name_tienda>[-\w]+))', MostrarTiendas.as_view(), name='mostrartiendas'),
Related
Im trying to redirect to a view with multiple parameters:
in my urls.py (app exhibition) I have:
path('map/<float:lat>/<float:lng>/<int:zoom>', views.MapView.as_view(), name='map')
where float is defined in a path converter like this:
'[-+]?\d*\.?\d*'
For the redirection I have:
return redirect('exhibition:map', lat=48.128365,long=11.5662713,zoom=3)
After hours of trying and researching similar questions I still get:
Reverse for 'map' with keyword arguments '{'lat': 48.128365, 'long': 11.5662713, 'zoom': 3}' not found. 1 pattern(s) tried: ['de/map\\/(?P<lat>[-+]?\\d*\\.?\\d*)\\/(?P<lng>[-+]?\\d*\\.?\\d*)\\/(?P<zoom>[0-9]+)$']
If I adjust the url pattern and the redirect call to a single pattern (be it float or int) the redirection works. Thus the problem should be related to my usage of multiple parameters - but I just can see what is wrong.
Any hints welcome!
i guess it's typo, you have defined lng as second parameter in your path but in redirect statement you call it long, it should be
return redirect('exhibition:map', lat=48.128365, lng=11.5662713, zoom=3)
I am trying to pass the first part of a django url to a view, so I can filter my results by the term in the url.
Looking at the documentation, it seems quite straightforward.
However, I have the following urls.py
url('<colcat>/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('<colcat>/', views.collection_view, name='collection_view'),
In this case, I want to be able to go to /living and have living be passed to my view so that I can use it to filter by.
When trying this however, no matter what url I put it isn't being matched, and I get an error saying the address I put in could not be matched to any urls.
What am I missing?
<colcat> is not a valid regex. You need to use the same format as you have for name.
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
Alternatively, use the new path form which will be much simpler:
path('<str:colcat>/collection/<str:name>', views.collection_detail, name='collection_detail'),
path('<str:colcat>/', views.collection_view, name='collection_view'),
There is urls.py pattern.
url(r'^notice/(?P<article>[0-9]\d+)/', web.views.notice),
Here is views.py
def notice(request, article):
data = article
return render(request, "notice.html")
However, web brower shows 404 Error.
If I remove add parameter, it is ok.
What I am wrong?
Intended result (Blog style, not get parameter)
/notice/1, /notice/2, ...
I think what is happening is that [0-9]\d+ is expecting at least a 2-digit number, one digit for the [0-9] and then one or more digits following that due to the \d+. I believe what you really want is just
url(r'^notice/(?P<article>\d+)$', 'web.views.notice')
I don't know why you use d???
url(r'^issue/(?P<issue_id>[0-9]+)$', views.issue, name='issue'),
url(r'^project/(?P<pk>.*)$', login_required(views.ProjectView.as_view()), name='project'),
Based on the question you asked, I am getting that you want to display the data on the template based on the parameter passed in the URL.Let me try to explain it step by step:
First lets say you have the following url:
url(r'^notice/(?P<article>\d+)$', views.notice,name="notice")
Now lets define the view for fetching the data from the model, based on the parameter in the URL, i am assuming you are passing the PK in the URL:
def notice(request, article):
data = YourModelName.objects.get(id=article)
//Passing back the result to the template
context={"article":data}
return render(request, "notice.html",context)
Now in your template you can access the data as such:
{{ article.field_name }}
Hope this helps you out!!!!
I had two methods create and update in the views, in which update takes one argument whereas create does not take any. I have decided to turn them into only one function update_create because they were not that different.
This is how the new method in views looks:
def update_create(request, id=None):
This is my urls.py:
url(r'^(\d+)/update/$|create/$', update_create, name='update_create'),
This is how my template looks in templates/list.html
Create a new event
I got this error when using the above code:
NoReverseMatch at /agenda/list/
Reverse for 'update_create' with arguments '()' and keyword arguments '{}' not found.
But, if I use this in my templates instead (I have added an argument), it works without any errors:
Create a new event
Can someone explain what's happening? Why previous code didn't work, and Why the new code is working?
URL pattern (\d+) expects number to be provided as argument. To resolve the issue simply provide urls like this:
url(r'^(\d+)/update/$', update_create, name='update_create'),
url(r'^update/$', update_create, name='update_create'),
As mariodev pointed out, your url pattern was expecting a digit in front of the url. As such, your first url:
Create a new event
would generate a url like /update, which wasn't a valid url. However, the latter url:
Create a new event
would generate a url like /1/update, which was a valid url.
From the django docs: https://docs.djangoproject.com/en/dev/topics/http/urls/
Basically subsequent arguments get parsed on first come first serve, and passed to your view. Another thing to consider when developing is using explicitly named parameters, as the django docs elaborate on.
In django, when a URL is matched, the match group is passed as a second parameter to the view function, the first being a HttpRequest object. For example, with a URL patter like this
'/foo/(\d{2})/', 'app.views.handler'
the handler routine will have
def handler(request, value):
where value will contain a two digit number (as a string).
My question is: is value also contained in the request object, and if yes, how can I get it (of course, parsing the URL from the request object is not an option, too impractical).
Thanks
I'm not going to debate the merit of your idea. Just try to answer your question:
No there is no way, other than applying the regex to the URL again, to get at the url parameter.
Your view will be the first point where the parameter list will be available. Why don't you just create a wrapper object to encapsulate your request and your parameter list at that point?
Just pass that around...
Can you give any reason why you would need this?
I don't see why parsing the url path is 'impractical', given that you've already got a regexp that works, in your urlconf.