When I run my django app on a production server, I receive an error stating that my urlconf does not contain any patterns. After a few hours of research, I discovered that my problem was mainly due to url template tags that were attempting to do a reverse lookup on the urlconf before the urlconf loaded. This always resulted in an error.
I think that reverse_lazy might solve this issue, as it'd force the urlconf to be loaded first, but I don't know how to apply this to function based views, as the solutions I found online only applied to Class based generic views. Is there a way to fix this problem using reverse_lazy? Or should I try something else?
def myview(request):
return HttpResponseRedirect(reverse('arch-summary', args=[1945]))
from the docs here: https://docs.djangoproject.com/en/1.6/ref/urlresolvers/ shows this usage for reverse. I've never tried it with reverse_lazy, but worth a shot.
Related
How do you return a url using get_success_url(self) in django without using reverse for a generic view? Basically, I want to have something as below.
def get_success_url(self):
url = self.object.url # This is a charfield with "https://stackoverflow.com/questions/ask"
return url
I know that people often use reverse when returning a url, but I would like do it like above. Thank you, and please write any questions you have. By the way, I never tested the above code, but I don't know if it will work properly.
"reverse(..)" is part of the django logic to store the url structure in urlpatterns in ONE place and then in the whole code refere to it in the moment of the request being executed via the name. If you do not want to follow that logic - feel free to store anything you like in your "object.url" and return it like you propose. You then just do not follow the django urlpattern idea. Up to you to decide if that is important or "right" in your case.
If the url targets outside our own server space, reverse is anyway not usefull as those urls are not part of your urlpattens.
I'm trying to wrap my head around Django concepts, but I struggle with the URLResolver reverse_lazy(). As far as I know, I have to use reverse_lazy() when I want to reverse to an URL that has not been loaded. So when I create a CBV and state a success_url, I use reverse_lazy(). This means that the URL is not imported when the file executes.
This is confusing to me because I think the server loads all the URLs while starting before executing anything. So how come the URL is not loaded in time of execution?
I would be very happy if someone would give me an answer to this.
Well, first of all, the URL resolver itself is lazy, so loading happens when the first call to resolve() or reverse() is made (usually on the first request). But that doesn't matter much in this case.
When the URL resolver is being initialised, it imports your URL configuration, which in turn imports your views. So at the time your view is imported and success_url is set, the resolver is only halfway through its initialisation. Calling reverse() at this point would not work since the resolver doesn't have all the information yet to reverse the view name.
I'm experiencing problems in routing urls to views in Django. Specifically, I use URLs with the pattern:
url(r'^(?P<id>[A-Za-z0-9\ ]+)/(?P<subid>[A-Za-z0-9\ ]+)/managetables$', views.compiledata, name='compiledata')
An example url would be My data/current/managetables. I checked that the regex returns the expected captured groups on www.pyregex.com (example)
However, actually visiting the url does not result in the view being called. Most importantly though, it works for a highly similar url:
url(r'^(?P<id>[A-Za-z0-9\ ]+)/(?P<subid>[A-Za-z0-9\ ]+)/managetab$', views.compiledata, name='compiledata')
If I visit My data/current/managetab the view is called as expected. Additionally, appending a "/" in the urlconf works also - but it is not clear to me why, i.e.:
url(r'^(?P<id>[A-Za-z0-9\ ]+)/(?P<subid>[A-Za-z0-9\ ]+)/managetables/$', views.compiledata, name='compiledata')
and visiting My data/current/managetablesresults in a redirect to My data/current/managetables/which calls the view.
I appreciate any hints how to solve this issue.
Ok, whereas the problem did manifest only on one of two machines, the hint to slugify the urls solved the issue. For anybody encountering similar issues, more information on slugify can be found here:
Tango with Django's Chapter 7, as well as in the Django Documentation.
I decorate my protected pages with a #login_required(login_url=login_url)
When, at the top of my views.py file, I set login_url=reverse("member_login") it throws an exception:
Tried tag_index in module dgn.views. Error was: 'module' object has no attribute 'tag_index'
tag_index is the last item in the URL dispatcher list. If I comment that out then it simply throws the exception mentioning the one before it.
The strange thing is, when I set the login_url="constant/string/to/path", then it works just fine. Obviously, login_url=reverse('member_login') is messing it up. I use reverse in many other places inside functions in the views.py file, and make sure to name each entry in the URL dispatch list.
Any help about what could be the problem would be much appreciated. Thanks in advance!
The URLConf is not loaded yet at that point.
Use reverse_lazy, if you have django 1.4
Are you importing anything from views.py in your urls.py file? If so you might have circular import issues, and you might need to rejig urls.py so it doesn't need the import.
I am busy with my group profile app and whenever i click on the create_group link i get this error instead of the create_group page. I think the problem may be in my URLS.PY file, has anybody ran into this problem. I am at work so will post some code in a few hours.
UPDATE:
The issue was in my URLS.py project file, i guess it wasn't syncing well with my urls.py app file. Such a small issue but it was really frustrating. Thats the life of a newbe!
I had same problem. URL localhost:8000/admin/auth/user/add/ created 404. Solution was obvious after I first found it.
My accounts app had missing ^ in accounts/url.py:
urlpatterns = patterns('',
url(r'user/(?P<username>\w+)/$', 'accounts.views.profile')
)
So I fixed it:
urlpatterns = patterns('',
url(r'^user/(?P<username>\w+)/$', 'accounts.views.profile')
)
The original matched with .../user/add/ before admin urls.py and caused the 404.
This is a very vague question, and without your code it's almost impossible to answer you correctly. However, I'll take a stab at it. It seems to me that you may be using the get_object_or_404 method in your view? Or is this an admin view? Either way, the answer is the same: You have a group profile and a reference to a user from that group that doesn't exist. It seems like you may just have data inconsistencies in your DB. I may be way off, it's hard to tell with so little information. If you give me more information to work off, I'll amend my answer to give you a more complete response.
One thing I can tell you for sure is that is NOT a missing URL error message. That error message is sending a 404 back because it can't retrieve an object from the DB. This would occur after the URL pattern was matched. However, I guess it's also possible that it's a user session issue, where you were logged in with a user that no longer exists, but that's just conjecture.
Good luck!