I have an issue similar to this one. My error message is something like this:
Reverse for 'detail' with arguments '('some-slug',)' not found. 1 pattern(s) tried: ['users/(?P<username>[^/]+)/$']
I intend to use some-slug and not ('some-slug',) for URL reversal. When I follow
Saifeddin's suggestion of using <str:slug>, it works. I then switch back to <slug:slug>, it also works. The problem simply vanishes. Could someone explain to me why Django (4.1.2) behave this way? I am at a loss.
Related
I'm trying to set up my first CRUD page using django and so far I've got to the D part, respectivly - delete. Following the information found on the internet, I tried this: SomeModel.objects.filter(id=id).delete(), but that threw out an error: id() takes exactly one argument (0 given) so could you tell me what am I doing wrong in order to make it work please?
Thank you.
SomeModel.objects.filter(id=id)[0].delete()
I'm trying to use TemplateView.as_view() in urls.py then name it using ye olde templatetag url. Should this work? Or am I just doing it wrong..? Or is it some of the legacy crap in my app interfering? I've got...
<li>Terms and Conditions</li>
and
url(r'^legal/$', TemplateView.as_view(template_name="legal.html"), name="legal"),
Which yields
NoReverseMatch at /how-it-works/
Reverse for '"legal"' with arguments '()' and keyword arguments '{}' not found.
Seems to me this ain't ever gonna work this way.
How do we reverse urls with TemplateView?
The code you have posted is syntactically correct, but there is something else interfering.
That url tag is Django 1.5+ syntax (note the warning in the docs here), can you confirm that is the install you are using? I suspect this is the the problem, the double quoting of legal in the exception message is very suspicious. A Django 1.5+ exception message would contain 'legal', not '"legal"' for a URL that is not found by the dispatcher.
Is the url being registered by the URL dispatcher? You can tell in debug mode by hitting a bad URL (ie 404), the output will list all valid url paths from your URLConf.
Add more info if you continue to have problems so we can isolate further.
Good luck!
I've configured url.py as is explained in different places of the Django documentation, but its behaviour is completely anomalous, between consecutives acceses. In Debug Mode and after looking for an url I get the "Page not found" error. The strange thing is that between acceses the error given is different:
... Django tried these URL patterns, in this order:
^admin/?$
^cheqsim/?$
^login/?$
^logout/?$
^questions/?$
But if I look at url.py my last element is ^searchquestions/?$ and I had restarted the server before looking at it, but the error is indicating me a different urlpattern from the actual in url.py.
Then I keep looking URLs and when I delete all the cookies and I try some non-existent URL I get the actual URL.py:
, Django tried these URL patterns, in this order:
^login/?$
^logout/?$
^admin/?$
^cheqsim/?$
^searchquestions/?$
Ok, so now I try http://mysite.com/myprojecturl/searchquestions/ and voila! It appears a new pattern error that leave me completely knock out:
, Django tried these URL patterns, in this order:
^admin/?$
^cheqsim/?$
^login/?$
^logout/?$
A pattern that I had used like 5 or 6 hours ago!!!
Please, do anybody have any idea of what is happening to me because this is really frustrating...
Thank you very much
P.S.: By the way, some URLs matches sometimes magicly
Check if you have multiple runserver instances running.
Check the ROOT_URLCONF setting
It seems the solution is changing the project.wsgi file and is well explained here: http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
Anyway only time will know... xD
I'm not sure if this is a bug with django-registration or what I'm doing.
I have a bare minimum project, with django-registration installed and no apps of my own. Django-registration requires a few templates, so I have them in templates/registration. In each template, I have a template tag {% url index %}, which is included in my urls.py.
By running ./manage.py test registration --failfast, I get:
TemplateSyntaxError: Caught NoReverseMatch while rendering: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
My own template isn't seeing my url entry.
I debugged into Django's reverse function, and it seems as though my urls.py isn't being used at all. Instead registration.tests.urls.py is used.
Is this the intended behavior? I'm hoping not, since I can't get basic tests to pass. How do I get around this?
This is a bug in django-registration. This blog article describes the problem and a workaround.
I'm developing a django application and over time, the URLs have grown. I have a lot of them with me now and due to some change I made, one view started to malfunction. When I try to GET http://example.com/foo/edit_profile, it's supposed to execute a view certain view function X but it's executing Y instead. Somewhere the url routing is messing up and I can't figure it out. I used the django.core.urlresolvers.resolve method to try it from the shell and I can confirm that the URL is getting wrongly resolved. However, I don't know how to debug this and pinpoint the problem.
Ideally, I'd like to see something like "tested this pattern", "tested this pattern" etc. till it finally finds the correct one and I can then look around where it resolved. I can't find anything like this.
Isn't this a common problem for larger projects? What do people do?
Update
I know how the system works and how to look through the URLs one by one. That's what I'm trying to do. This question is basically asking for a shortcut.
have you already tried to run
manage.py show_urls
after installing django_extensions?
http://vimeo.com/1720508 - watch from 06:58.
This should give you in what order the url resolution is attempted.
Hope this helps
I would comment out the patterns in your url.py until you get a 404 error when you try to navigate to foo. If that pattern is an include, I would recurse down that and comment out lines in that url.py. Eventually you will know exactly which pattern is matching.
Then I would take the view function that it is calling and hard code that. If it is using a generic view or something subtle, I'd make it as obvious and direct as possible. At that point, you should know which rule is matching and why and what code it is executing in the view.
You can assume, that it goes through the urlpatterns from top to bottom and the first one that matches will be executed.
As you know which view is executed (Y) think about that:
if Y is before X: the patterns of Y matches the url (but shouldn't)
if X is before Y: the patterns of X doesn't match the url (but should)
Can you provide some more explicit examples of your URLConf? Than I can give you a more explicit answer.
Look at your urlconfs, find which urlpattern invokes your view Y and see if the regexp is more general than it ought to be. Try to comment out the urlpattern which causes the false match and see if it correctly matches X.
Typically, this is not a problem for me, but it does occur. Always keep more specific patterns before general ones. Use static prefixes to divide your url namespace, to prevent false matches.