I have a weird edge-case where I need to use the data stored in urls.py within a view, but since the urls.py file imports the view, I am getting a circular import error when I try to import the url data into the view.
cannot import name 'url_patterns'
Does Django have any way of grabbing all the url patterns within a view that could circumvent this error? The fact that the reverse() function exists indicates that it might.
You can solve this typically (well it depends a bit), by importing the urls in your view(s) locally. Like:
# urls.py
import someapp.views
url_patterns = [
url('some_url/', someapp.views.some_view),
]
and then import the urls in the views like:
# views.py
def some_view(request):
from someapp.urls import url_patterns
# ...
# do something with urlpatterns
# return response
pass
We here thus postpone the importing of the urls.py in the views, until the view function is actually called. By that time both the views.py and urls.py are (usually) already loaded. You can however not call the view in the views.py directly, since then the import is done before the urls.py is loaded.
Note that usually you do not have to import url_patterns, and you can use reverse(..) etc. to obtain a URL that corresponds to a view function (even passing parameters into a dictionary, etc.). So I really advice to look for Django tooling to handle URLs.
Found an answer:
from django.urls import get_resolver
url_patterns = set(v[1] for k,v in get_resolver(None).reverse_dict.items())
Related
I am using i18n_patterns but I want to use reverse to create a link to the page without language in the url (such that the user will be redirected based on cookies and headers and such).
I have tried
from django.utils.translation import activate, deactivate, get_language
current_lang = get_language()
deactivate()
url = reverse(things)
activate(current_lang)
That works for getting other language versions using activate(target_lang), but if I deactivate I just get urls for the default language (/en/account/ but I want /account/).
I already thought getting alternate language versions is overly complicated, but this I cannot manage at all. Any hints? (Without manually stripping LANGUAGE_CODE from the url)
UPDATE: I also tried
from django.core.urlresolvers import get_resolver
get_resolver(None).reverse(*args, **kwargs)
but get NoReverseMatch
I think the easiest way is to let Django resolve the URL with the language prefix and then just remove the language prefix.
You can write the following function:
import re
from django.core.urlresolvers import reverse
def reverse_no_i18n(viewname, *args, **kwargs):
result = reverse(viewname, *args, **kwargs)
m = re.match(r'(/[^/]*)(/.*$)', result)
return m.groups()[1]
Now, anywhere in your code you can do something like this:
from myproject.utils import reverse_no_i18n
def my_view(request):
return HttpResponseRedirect(reverse_no_i18n('my_view_name'))
You might also want to create a custom {% url %} templatetag which calls your custom function.
I also spent time to find a nice solution and here is mine.
Next to main urls file ('my_project/urls.py'), create the file 'my_project/urls_without_lang.py' with the content below.
Then, you can use reverse('viewname', urlconf='my_project.urls_without_lang')
Django=<1.11
from copy import copy
from django.urls.resolvers import LocaleRegexURLResolver
from .urls import urlpatterns as urlpatterns_i18n
"""
Purpose of this file is to be able to reverse URL patterns without language prefix.
This is usefull to build URL meant to be communicated "outside" of the domain without any language duty.
To use it with 'reverse' method (from django.shortcuts module), simply give the additional parameter:
`urlconf='my_project.urls_without_lang'`
Example: `reverse('viewname', urlconf='my_project.urls_without_lang')`
"""
urlpatterns = copy(urlpatterns_i18n)
for el in urlpatterns_i18n:
if isinstance(el, LocaleRegexURLResolver):
urlpatterns.remove(el)
urlpatterns += el.url_patterns
Django>1.11
from copy import copy
from django.urls import URLResolver
from .urls import urlpatterns as urlpatterns_i18n
urlpatterns = copy(urlpatterns_i18n)
for el in urlpatterns_i18n:
if isinstance(el, URLResolver) and isinstance(el.urlconf_name, list):
urlpatterns.remove(el)
urlpatterns += el.url_patterns
Hope that will help some of you.
I am working on a big news publishing platform. Basically rebuilding everything from ground zero with django. Now as we are almost ready for the launch I need to handle legacy url redirects. What is the best way to do it having in mind that I have to deal with tenths of thousands of legacy urls?
Logic should work like this: If none of existing urls/views where matched run that url thorough legacy Redirect urls patterns/views to see if it can provide some redirect to the new url before returning 404 error.
How do I do that?
You may want to create a fallback view that will try to handle any url not handled by your patterns. I see two options.
Just create a "default" pattern. It's important to this pattern to
be the last within your urlpatterns!
in your urls.py:
urlpatterns = patterns(
'',
# all your patterns
url(r'^.+/', 'app.views.fallback')
)
in your views.py:
from django.http.response import HttpResponseRedirect, Http404
def fallback(request):
if is_legacy(request.path):
return HttpResponseRedirect(convert(request.path))
raise Http404
Create a custom http 404 handler.
in your urls.py:
handler404 = 'app.views.fallback'
in your views.py
from django.http.response import HttpResponseRedirect
from django.views.defaults import page_not_found
def fallback(request):
if is_legacy(request.path):
return HttpResponseRedirect(convert(request.path))
return page_not_found(request)
it may seem to be a nicer solution but it will only work if you set DEBUG setting to False and provide custom 404 template.
Awesome, achieved that by using custom middleware:
from django.http import Http404
from legacy.urls import urlpatterns
class LegacyURLsMiddleware(object):
def process_response(self, request, response):
if response.status_code != 404:
return response
for resolver in urlpatterns:
try:
match = resolver.resolve(request.path[1:])
if match:
return match.func(request, *match.args, **match.kwargs)
except Http404:
pass
return response
Simply add this middleware as a last middleware in MIDDLEWARE_CLASSES list. Then use urls.py file in your legacy app to declare legacy urls and views which will handle permanent redirects. DO NOT include your legacy urls in to main urls structure. This middleware does it for you, but in a bit different way.
Use the Jacobian's django-multiurl. There is a django ticket to address the issue someday, but for now django-multiurl works very good.
Before:
# urls.py
urlpatterns = patterns('',
url('/app/(\w+)/$', app.views.people),
url('/app/(\w+)/$', app.views.place), # <-- Never matches
)
After:
# urls.py
from multiurl import multiurl, ContinueResolving
from django.http import Http404
urlpatterns = patterns('', multiurl(
url('/app/(\w+)/$', app.views.people), # <-- Tried 1st
url('/app/(\w+)/$', app.views.place), # <-- Tried 2nd (only if 1st raised Http404)
catch=(Http404, ContinueResolving)
))
Situation in short. For some reason reverse() method does not work.
in PROJECT urls.py
url(r'^enrollment/', include('project.enrollment.urls')),
in APP urls.py
url(r'^thanks/$', 'enrollment.views.thanks', name='enroll_thanks'),
and in views.py
from django.core.urlresolvers import reverse
def thanks(request):
return render_to_response('enrollment/thanks.html', {}, context_instance=RequestContext(request))
def enroll(request):
''' some code for handling the form'''
return HttpResponseRedirect(reverse('enrollment.views.thanks'))
This reverse causes following error:
Could not import project.views. Error was: No module named views
in file ../django/core/urlresolvers.py in _get_callback, line 167
Any idea why this does not work? Next step is to call the thanks-view with a parameter, but that should be easy after this setup works. Should there be something more to be imported in views.py?
From the docs for reverse: "As part of working out which URL names map to which patterns, the reverse() function has to import all of your URLconf files and examine the name of each view. This involves importing each view function. If there are any errors whilst importing any of your view functions, it will cause reverse() to raise an error, even if that view function is not the one you are trying to reverse."
Are any of your urls referencing project.views....?
Not too sure of your module layout but based on your include() line in urls.py it looks like you may want to add "project." to the argument in your reverse() call as well.
reverse('project.enrollment.views.thanks')
That or you may have a bug in the view.
I have a html file ('search.html') with a form on it. I have saved it to ~/Django/Templates just for the sake of argument. The Django book said it doesn't matter where I save it, because the framework will find it. Anyway, I have set up a function in the views.py file to render this file. Here it is:
from django.http import HttpResponse
from django.shortcuts import render_to_response
def search(request):
return render_to_response('search.html')
I have this function called in the urls.py file as well:
urlpatterns = patterns('',
(r'^$', index),
(r'^search/$', search),
However, whenever I go to visit the page with the ~/search in the URL, I get the following:
TemplateDoesNotExist at /search/
What's the problem?
In your settings.py file there is a line...
TEMPLATE_DIRS = (...)
You want to make sure the directory containing the template is in that tuple.
I try to add a recaptcha field to my registration form and followed Marcos guide:
http://www.marcofucci.com/tumblelog/26/jul/2009/integrating-recaptcha-with-django/
In my registration app, I have a file "forms.py" which looks like this:
from recaptcha import fields as captcha_field
from registration.forms import RegistrationFormUniqueEmail
class RecaptchaRegistrationForm(RegistrationFormUniqueEmail):
recaptcha = captcha_field.ReCaptchaField()
and a urls.py which gets included under /accounts by my solution wide urls.py:
from django.conf.urls.defaults import *
from registration.views import register
from forms import RecaptchaRegistrationForm
urlpatterns = patterns('users.views',
(r'^$', 'profile'),
url(r'^register/$', register, {'form_class': RecaptchaRegistrationForm}, name='registration_register'),
)
Now, when I go to /accounts/register/ I get this error message:
Exception Value: register() takes at least 2 non-keyword arguments (1 given)
I have no idea why.
The first non-keyword argument it's asking for is request, which is gets automatically.
The second non-keyword argument, which it isn't getting, is the authentication backend.
To get going quickly you can just use the default backend that comes with django-registration. I can't easily test this myself, but this should do it:
from django.conf.urls.defaults import *
from registration.views import register
from forms import RecaptchaRegistrationForm
from registration.backends.default import DefaultBackend
urlpatterns = patterns('trackerbase.users.views',
(r'^$', 'profile'),
url(r'^register/$', register, {
'backend': DefaultBackend,
'form_class': RecaptchaRegistrationForm,
}, name='registration_register'),
)
Take a look at the file you link to starting at line 95. Reading over that should tell you all you need to know.
You can use recaptcha-client, For step by step procedure you can follow k0001's
blog it works out of the box.
'backend' isn't an optional argument.
Can you please attach the stack trace of your exception?
It seems like it's trying to use DefaultBackend as a string.