NoReverseMatch Error on Django Redirect - python

I am getting the following exception in movie app of my django project:
Exception Type: NoReverseMatch at /movie/
Exception Value: Reverse for 'movie.views.movie_detail' with arguments '(u'the_peanuts_movie',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Here are the url patterns of movie.urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<movie_name>[\w_]+)$', views.movie_detail, name='movie_detail'),
url(r'^', views.movie_home, name='movie_home'),
]
Here are some parts of the movies.views.py:
def movie_detail(request, movie_name):
# code
...
def movie_home(request):
...
return redirect(movie_detail, movie_name)
# movie_name is a string
So far, I cannot make this code work. But it works, if I change return statement and put this in movie_home function:
movie_name += '/'
return redirect(movie_name)
Now after reading a few other questions of this type in stackoverflow, I know that the return redirect(movie_name) statement works since it redirects to an url:
127.0.0.1:8000/movie/whatever_is_the_movie_name
But I do not understand why redirecting to movie_detail view is not working.
How can I make it work?

First of all, movie_detail should be a string ('movie_detail') and under the 'movie' namespace - also, try using reverse and passing in the name as an arg:
return redirect(reverse('movie:movie_detail', args=[movie_name]))
Also, it looks like you missed an ending slash in your urls.py before the $:
url(r'^(?P<movie_name>[\w_]+)/$', views.movie_detail, name='movie_detail'),

Try This:
movie.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<movie_name>[\w_]+)$', views.movie_detail, name='movie_detail'),
url(r'^', views.movie_home, name='movie_home'),
]
movie.views.py
def movie_detail(request, movie_name):
# code
...
def movie_home(request):
...
return redirect('movie:movie_detail', movie_name) #CHANGES : (appname:url_name as str)
# movie_name is a string

Related

Get url variable and value into urlpatterns in Django

I was trying to get the variable and value of a url in urlpatterns in Django. I mean, I want to put in the address of the browser type: https://place.com/url=https://www.google.es/... to be able to make a translator. And be able to pick up the variable and value in the function that receives. At the moment I'm trying to get it with re_path like this:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index),
re_path('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', views.index_traductor),
]
The regex match picks it up, but I don't know how to send it as a value in a variable to receive here:
from django.http import HttpResponse
def index(request):
return HttpResponse("flag")
def index_traductor(request, url=''):
return HttpResponse("%s" % url)
I get a blank page. Any ideas?
Uh, no need for regex - why not just use get parameters?
URL:
https://place.com/?param1=val1
views.py
def my_view_function(reuqest):
# unpack get parameters:
val1 = request.GET.get('param1')
# do something ...

While creating dynamic url I am getting a Reverse for 'dashboard_group' with arguments error

urls.py
urlpatterns = [
path('processes/', views.processes, name="dashboard"),
path('processes/<uuid:u_id>/', views.groups, name="dashboard_group")
]
views.py
def processes(request):
return render(request, 'processes/index.html')
def groups(request, u_id):
return render(request, 'create_instance/index.html')
The above code gives me following error - Reverse for 'dashboard_group' with arguments '('a21713b0ec29416c8fb27d4f339eabb8',)' not found. 1 pattern(s) tried: ['processes\/(?P[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\/$']
Remove uuid: from the path. Your code should look like this.
urlpatterns = [
path('processes/', views.processes, name="dashboard"),
path('processes/<u_id>/', views.groups, name="dashboard_group")
]
According to Django official documentation, the bracket may include a converter specification (like the int part of <int:section>) which limits the characters matched and may also change the type of the variable passed to the view.
django.urls functions for use in URLconfs

Newbie Django Issue

I'm getting this error:
NoReverseMatch at /genomics/ Reverse for 'pipelinedetail' with
arguments '('02152ad7-8399-441c-ba8f-f8871460cd5f',)' and keyword
arguments '{}' not found. 0 pattern(s) tried: []
When I navigate to this URL:
http://127.0.0.1:8000/genoa/
My genoa_urls.py has:
urlpatterns = [
# ex: /polls/samples
url(r'^$', views.samples, name='samples'),
url(r'^(?P<sequence_id>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})/pipelinedetail/$', views.pipelinedetail, name='pipelinedetail'),
]
The offending line in my template is:
<td>JSON</td>
And my view contains:
def pipelinedetail(request, sequence_id):
# sequence_id = '7e6bd861-934f-44be-872a-b59826107bda'
sample = get_object_or_404(Sample, pk=sequence_id)
sample_details = sample.values('pipeline_detail')
context = {'sample_details': sample_details}
return render(request, 'polls/pipelinedetail.html', context)
Here's the top level urls.py:
urlpatterns = [
# polls in the url maps to polls.urls.py
url(r'^polls/', include('polls.urls')),
url(r'^genoa/', include('polls.genoa_urls')),
url(r'^admin/', admin.site.urls),
]
What am I doing wrong?
Your polls URLs are not in a namespace, so you don't need the prefix.
{% url 'pipelinedetail' me.sequence_id %}
Both of your suggestions ultimately worked as did my original code once I created a new app and placed my new code there. Thank you.

How can I handle query "?" in my django urls.py

I am new to Django. I have to write a moke. My server will look at a specific address.
Like this:
portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999
I wrote:
urls.py
from django.conf.urls import patterns, url
from rt_moke import views
urlpatterns = patterns('',
url(r'code=(?P<code_id>\w+)/', views.Sapata, name='sapata'),
)
and views.py
from django.http import HttpResponse
status = {u"99999": u'{"code": "99999","status": "undelivered"}',\
u"88888": u'{"code": "88888","status": "delivered"}',\
}
def Sapata(request, code_id):
return HttpResponse(status[code_id])
When I request for portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999 without ? mark - it works, and with ?- not. I understand, that it is query string and Django skips it in the regexp. So what can I do?
This URL:
portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999
has two parts, the path:
portal/client_api.ashx
and the query string:
client=SAPRA&key=1234234&func=status&code=99999
which is parsed into request.GET.
In views.py you should get params from request (like simple dict in request.GET), for example:
def test(request):
code = request.GET.get('code') # here we try to get 'code' key, if not return None
...
and of course, we can't use GET params to parse URLs in urls.py. Your urls.py should looks like:
from django.conf.urls import patterns, url
from rt_moke import views
urlpatterns = patterns('',
url(r'^portal/client_api\.ashx$', views.Sapata, name='sapata'),
)
P.S. Please, don't use capital letters in names of functions.

Django Template Reverse URL Resolution Not Working

I have the following in my project's urls.py:
urlpatterns = patterns('',
url(r'^watches/(?P<object_id>\d+)/$', list_detail.object_detail, watch_detail, name='watch_detail'),
)
However, the following line in a template returns an error:
<li>A link</li>
It returns this error:
NoReverseMatch at /watches/
Reverse for ''watch_detail'' with arguments '(1,)' and keyword arguments '{}' not found.
This confuses me a lot, because if I run "manage.py shell" I get the following results:
>>> from django.core.urlresolvers import reverse
>>> reverse("watch_detail", args=(1,))
'/watches/1/'
>>>
Any suggestions as to what might be wrong?
Thank you.
What's the third parameter you've got in your url conf (watch_detail)? Looking at the docs, your third parameter should be a dictionary.
Should your conf file read as follows? -
urlpatterns = patterns('',
url(r'^watches/(?P<object_id>\d+)/$', 'list_detail.object_detail.watch_detail', name='watch_detail'),
)
(that's assuming your view is at list_detail/object_detail/watch_detail).
Just to clarify, you can also pass a view function instead of a string path, so your url conf could be written like -
from your_app.your_module import watch_detail
urlpatterns = patterns('',
url(r'^watches/(?P<object_id>\d+)/$', watch_detail, name='watch_detail'),
)
Where the second parameter, watch_detail, is your view function.
EDIT
If watch_detail really is a parameter then you'll need to include it in the template reverse function -
{% url 'watch_detail', 1, watch_detail %}

Categories