Changing a old Django URL to the new paths - django

So I am making a new site in Django 2.0 and was following this tutorial on making a user registration form with an activation email and my understanding of the new Django 2 is not good enough so was asking what would be the Django 2 equivalent of this URL
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate'),

There is no straight conversion for your path you could either use a converter as stated in the docs to convert the token. Here the example from the docs:
class FourDigitYearConverter:
regex = '[0-9]{4}'
def to_python(self, value):
return int(value)
def to_url(self, value):
return '%04d' % value
register the converter
from django.urls import path, register_converter
from . import converters, views
register_converter(converters.FourDigitYearConverter, 'yyyy')
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<yyyy:year>/', views.year_archive),
...
]
or you can just regex the path like you currently are:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate')
]
I would just stick to the regex using re_path since you know it works and its already done.
Here is the link to the docs:
https://docs.djangoproject.com/en/2.0/topics/http/urls/

Related

Utilizing Django url resolver vs. re-inventing the request.path parsing wheel

Let's say I had the following array:
sub_urls = [
'/',
'/<uuid:id>',
'/test',
'/test/<uuid:id>'
]
The URL stings are very similar to what you'd find in Django's urlpatterns
my question:
can django.url.resolve be used to find the pattern in the sub_urls array given a path string like /test/189e8140-e587-4d5d-ac5c-517fd55c67bc without me having to re-invent the wheel here?
PLEASES NOTE: this isn't a question about how I route from urls to views in Django. Please don't tell me how to "solve it with urlpatterns, re_path etc." This is about utilizing this same mechanism by which django matches up a URL with a view --- for a COMPLETELY different purpose. I can write this myself, but I wanted to know if there's a way to re-use what Django has as it CLEARLY has the same needs when parsing request.path into the correct view.
Since django.urls.resolve calls get_resolver which creates a URLResolver (through _get_cached_resolver), you can instead create a URLResolver yourself:
# Define an empty view
def empty_view(*args, **kwargs):
return None
# Define the urlpatterns
from django.urls import path
sub_urls = [
'/',
'/<uuid:id>',
'/test',
'/test/<uuid:id>'
]
urlpatterns = [path(u, empty_view) for u in sub_urls]
# Create a resolver
from django.urls.resolvers import URLResolver, RegexPattern
resolver = URLResolver(RegexPattern(r'^'), urlpatterns)
# Match a path
# Returns a django.urls.resolvers.ResolverMatch object
result = resolver.resolve('/test/189e8140-e587-4d5d-ac5c-517fd55c67bc')
print(result)
print(result.route)
The above code will output:
ResolverMatch(func=__main__.empty_view, args=(), kwargs={'id': UUID('189e8140-e587-4d5d-ac5c-517fd55c67bc')}, url_name=None, app_names=[], namespaces=[], route=/test/<uuid:id>)
/test/<uuid:id>
If you're set on using django.urls.resolve, you could move urlpatterns to a module, let's say my_urls:
# Define an empty view
def empty_view(*args, **kwargs):
return None
# Define the urlpatterns
from django.urls import path
sub_urls = [
'',
'<uuid:id>',
'test',
'test/<uuid:id>'
]
urlpatterns = [path(u, empty_view) for u in sub_urls]
Notice we had to remove the / suffixes from the URLs above because _get_cached_resolver creates a RegexPattern that starts with /.
And then pass the module name as the second argument to resolve:
# Configure settings (only needed when this is not being run in an existing Django app)
from django.conf import settings
settings.configure()
# Match a path
# Returns a django.urls.resolvers.ResolverMatch object
from django.urls import resolve
result = resolve('/test/189e8140-e587-4d5d-ac5c-517fd55c67bc', urlconf='my_urls')
print(result)
print(result.route)
I don't believe the caching of _get_cached_resolver should be an issue as #Clepsyd mentioned since functools.lru_cache caches depending on the arguments it receives.

How to reverse path for custom user when testing Django Rest Framework

I'm building a Django 3 app using a Custom User. I'm trying to test the Custom User, but I can't get the url using reverse. I'm using Django Rest Framework. I'm using Django Rest Auth.
myapp.api.urls.py:
app_name = 'myApp'
from django.urls import include, path, re_path
urlpatterns = [
...
path('users/', include('users.urls'), name='UsersURLS'),
]
myapp.users.urls.py
from django.urls import include, path
from . import api
app_name = 'users' # namespace for reverse
urlpatterns = [
path('', api.UserListView.as_view(), name='UsersPath'),
]
myapp.users.api.py
class UserListView(generics.ListCreateAPIView):
queryset = models.CustomUser.objects.all()
serializer_class = serializers.UserSerializer
authentication_classes = (TokenAuthentication,)
And in test_users_api.py:
from users.api import UserListView
user_detail_url = reverse('myApp:UsersURLS-list')
...
def test_user_detail(self):
self.client.force_authenticate(user=self.user)
response = self.client.post(user_detail_url, {'pk': self.user.id }, format='json')
Whatever I try for the reverse, I get an error that is it not a valid view or function name. So far I have tried:
reverse('myApp:UsersURLS-list')
reverse('users:UsersPath-list')
reverse(UserListView)
reverse('UsersPath')
I'd be grateful for any idea what I'm doing wrong, and how to get the reverse URL. I have it working for the rest of my endpoints which use router, but I can't see what's needed for my Custom User, which I set up following a tutorial and it works fine otherwise.
I found an answer in the rest-auth source code, at venv36/lib/python3.6/site-packages/rest_auth/tests/test_api.py, and mixins.py in the same folder. I don't know why my attempts to reverse fail, but this works:
user_detail_url = reverse('rest_user_details')
All names for reverse can be found in the mixins.py file.

Django writing view with sudomain

With my (Django v 1.17) project I am using django-subdomains.
I have no problem to call index view and when I open my url https://subdomain.domain.com I will get index.html.
My issue that I wrote a new view called example for the sub-domain but when I open the url https://subdomain.domain.com/exmaple I will get error Page not found (404).
Hete is my code:
settings.py
INSTALLED_APPS = [
'subdomain'
]
SUBDOMAIN_URLCONFS = {
'subdomain': 'subdomain.urls',
}
subdomain/urls.py
from django.conf.urls import url, include
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$example', views.example, name='example'),
]
subdomain/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
def index(request):
template = loader.get_template('subdomain/index.html')
return HttpResponse(template.render())
def example(request):
template = loader.get_template('subdomain/example.html')
return HttpResponse(template.render())
Error:
Page not found (404)
Request Method: GET
Request URL: https://subdomain.domain.com/example
Using the URLconf defined in subdomain.urls, Django tried these URL patterns, in this order:
1. ^$ [name='index']
2. ^$example [name='example']
The current path, econ, didn't match any of these.
Please advise how to fix this issue and write view for sub-domain.
This is unrelated to django-subdomains. The dollar should be at the end of the regex.
url(r'^example$', views.example, name='example'),
The dollar matches the end of the string, so if you have it at the beginning then it's not going to match.

Recursive URL routing in Django

I want to have a (fairly simple) emulation of SELECT queries through URLs.
For example, in a blogging engine, You'd like /tag/sometag/ to refer to the posts having the sometag tag. Also /tag/sometag/or/tag/other/and/year/2013 should be a valid URL, beside other more complex urls. So, having (theoretically) no limit on the size of the url, I would suggest this should be done recursively, but how could it be handled in Django URL Routing model?
I would use a common URL pattern for all those URLs.
url(r'^query/([\w/]*)/$', 'app.views.view_with_query'),
You would receive all the "tag/sometag/or/tag/other/and/year/2013" as a param for the view.
Then, you can parse the param and extract the info (tag, value, tag, value, year, value) to make the query.
update 2021
django.conf.urls.url is deprecated in version 3.1 and above. Here is the solution for recursive url routing in django:
urls.py
from django.urls import path, re_path
from .views import index
urlpatterns = [
re_path(r'^query/([\w/]*)/$', index, name='index'),
]
views.py
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request, *args, **kwargs):
print('-----')
print(args)
print(kwargs)
return HttpResponse('<h1>hello world</h1>')
If I call python manage.py run server, and go to 'http://127.0.0.1:8000/query/nice/', I can see these in terminal:
-----
('nice',)
{}
I know this is an old question but for those who reach here in future, starting from django 2.0+, you can use path converter with path to match the path in the url:
# urls.py
urlpatterns = [
path('query/<path:p>/', views.query, name='query'),
]
# views.py
def query(request, p):
# here, p = "tag/sometag/or/tag/other/and/year/2013"
...

Redirect to named url pattern directly from urls.py in django?

In Django, how can I do a simple redirect directly from urls.py? Naturally I am a well organized guy, favoring the DRY principle, so I would like to get the target based on it's named url pattern, rather than hard coding the url.
If you are on Django 1.4 or 1.5, you can do this:
from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(url=reverse_lazy('my_named_pattern'), permanent=False)),
...
If you are on Django 1.6 or above, you can do this:
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(pattern_name='my_named_pattern', permanent=False)),
...
In Django 1.9, the default value of permanent has changed from True to False. Because of this, if you don't specify the permanent keyword argument, you may see this warning:
RemovedInDjango19Warning: Default value of 'RedirectView.permanent' will change from True to False in Django 1.9. Set an explicit value to silence this warning.
This works for me.
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(url='/')),
...
In above example '/' means it will redirect to index page,
where you can add any url patterns also.
for django v2+
from django.contrib import admin
from django.shortcuts import redirect
from django.urls import path, include
urlpatterns = [
# this example uses named URL 'hola-home' from app named hola
# for more redirect's usage options: https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/
path('', lambda request: redirect('hola/', permanent=False)),
path('hola/', include("hola.urls")),
path('admin/', admin.site.urls),
]
I was trying to redirect all 404s to the home page and the following worked great:
from django.views.generic import RedirectView
# under urlpatterns, added:
url(r'^.*/$', RedirectView.as_view(url='/home/')),
url(r'^$', RedirectView.as_view(url='/home/')),
This way is supported in older versions of django if you cannot support RedirectView
In view.py
def url_redirect(request):
return HttpResponseRedirect("/new_url/")
In the url.py
url(r'^old_url/$', "website.views.url_redirect", name="url-redirect"),
You can make it permanent by using HttpResponsePermanentRedirect
You could do straight on the urls.py just doing something like:
url(r'^story/(?P<pk>\d+)/',
lambda request, pk: HttpResponsePermanentRedirect('/new_story/{pk}'.format(pk=pk)))
Just ensure that you have the new URL ready to receive the redirect!!
Also, pay attention to the kind of redirect, in the example I'm using Permanent Redirect