I am following an older tutorial on creating Django apps. In the section on modifying page URLs the tutorial uses a Regular Expression with a capturing group to pass the parameter in the URL to views.
I am using Django 2.0, so I am using path rather than url, and I am wondering what the correct substitution for the regular expression is. I have gotten around it for now by using re_path, but I'd like to know how it should actually be done in Django 2.0.
The old code is:
url(r'([^/]*)', views.index, name='index'),
I have made numerous attempts to adapt this to path, including (pagename is the parameter in the index function):
path('/<pagename>', views.index, name='index'),
path('/<str:pagename>', views.index, name='index'),
path('/<str:pagename>/', views.index, name='index'),
etc. None of the permutations I could come up with worked and I got a 404 Error every time.
Let us first take a look at the url(..):
url(r'([^/]*)', views.index, name='index'),
This means that you accept every path with zero or more characters that are not slashes (that is what the [^/] means, it means a character group that contains all but the slash character, and the Kleene star * means zero or more repititons).
Now typically in Django one adds a slash at the end, but the path should not begin with a slash. So a path(..) that should work is:
path('<str:pagename>/', views.index, name='index'),
Since str does not accept a slash as well, the two are now equivalent, except for the fact that str should contain at least one character. We can for example use two path(..)s to include the empty string as well:
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
Related
I am working on a project that generates dynamic urls
for eg if you type mysite.com/<yourtexthere> The site should generate a url with mysite.com/yourtexthere (where yourtext here is a slug of a model)and I am able to do that but the problem arises when I put something like this mysite.com/yourtexthere/moretext, Django doesn't match it with any of my existing URL patterns and gives me 404.
I wanted to ask is there a way by which I can treat '/' as just another character and generate unique url mymysite.com/yourtexthere/moretext where yourtexthere/moretext is now the slug.
views.py
def textview(request, slug):
obj, created= Text.objects.get_or_create(slug=slug, defaults={'text':'', 'password':'123'})
return render(request, 'text/textpage.html', {'obj' : obj, 'created' : created})
urls.py
# Only patterns
urlpatterns = [
path('', home, name='home'),
path('<slug:slug>/', textview, name='textview'),
]
From Django models docs:
A slug is a short label for something, containing only letters, numbers, underscores or hyphens.
So the 404 is actually correct, maybe use another field.
Django path converters match strings in the input URL using regex.
The default path converters are pretty basic - source code.
The slugConverter matches any string that only contains characters, numbers, and dashes, not forward slashes. In string yourtexthere/moretext the largest substring it will match is yourtexthere.
The pathConverter matches a string containing any type of character, so the result can contain a forward slash. It will match all of yourtexthere/moretext. So change your urlpatterns to this:
# Only patterns
urlpatterns = [
path('', home, name='home'),
path('<path:slug>/', textview, name='textview'),
]
The pathConverter will match all special characters. If you don't want this you can create your own custom converter with a regex tailored to your needs. For example, you could simply extend the slugConverter to also match strings with forward slashes.
class SlugPathConverter(StringConverter):
regex = '[-a-zA-Z0-9_/]+'
I'm having an issue with Django URL patterns.
When I add a '/' to the end of the index URL, the page returns a 404 error (Page Not Found) and if I remove the '/' from the end of the URL then the page works fine.
The issue is not reproducible with the URL for the admin page, can someone explain what's going on?
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/',index),
]
$: Represents end of string, so, there's no posibility for a char living after it.
Matches the end of the string or just before the newline at the end of
the string
^ matches the start of the string so, ^$ in root urls.py means to Django: I don't want anything in my URL except the domain / base name then Django will route the request to your index page.
url method of django.conf.urls package accept regex as first parameter
$ represent end of string in regular expression hence any char after that will not be considered to match url string.
As I understood , in django urls:
'$' : End of the string, this is use in regex
'^' : It is also use in regular expression is matching starting of url
no characters after $ will be considered in the url pattern
hence change this to :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/$',index),
]
I want my url to accept 0 or more digits(positive or negative integers), I mean it should match '/','/0',...,'/9' as well as '/-9','/-88' etc.
This is the regex I am using ^([-]?[0-9]*)/$ . It works for all urls except '/', what is the problem with this regex?
EDIT:
This is my urlpatterns in urls.py in project directory:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('basecalendar.urls'), name='date'),
]
and this is urlpattern for basecalendar
urlpatterns=[
url(r'^([-]?[0-9]*)/$',views.get_date),
]
Since you are working with urls you might want to make sure that you are ending your url with '/'. Also, the reason why this is not working because your url is expecting a '/' at the end. So a url something/ does not match your regex, rather something// does. All these observations are being made according to your regex. Usually to handle such conditions you should add one more url above your previous regex, something like:
url(r'^something/$', view),
url(r'^something/([-]?[0-9]*)/$', view),
Okay, to be clear I've searched and read, followed the official docs, tried multiple solutions from SOF, nothing seems to be working so I have to resort to shamefully asking for help.
I'm simply trying to generate urls the proper way.
root urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'|/', include('main.urls')),
]
urls.py:
app_name = 'main'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^vieworder/(?P<order_id>[0-9]+)/$', views.vieworder, name='vieworder'),
]
template file:
<td>View</td>
also tried:
<td>View</td>
Error:
Reverse for 'vieworder' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['|/vieworder/(?P<order_id>d+)/$']
I don't understand why this is not working. I tested the regex against vieworder/1/ in a regex tester and it works fine. Django even tells me in the error that it tried the correct url pattern, however the error really isn't very clear on what is actually wrong.
Django is not able to reverse the use of a | character outside of a capturing group. However, I highly doubt you need it here.
Django always matches the first slash of the url, so there's no need to match the starting slash in your regex. Adding a starting slash would only match a second slash at the start of your url. Unless you want the url path to be example.com//vieworder/1/ rather than example.com/vieworder/1/, you should just remove the slash from your pattern.
Since the first slash is already matched by Django, and there's nothing else between the first slash and the vieworder/1/ part, you can just leave the include pattern empty:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('main.urls')),
]
This will match the url example.com/vieworder/1/, and allow Django to reverse the url.
As for your second problem:
You need to make the outer group a non-capturing group with ?::
url(r'^vieworder(?:/(?P<order_id>\d+))*/$', views.vieworder, name='vieworder'),
Django will substitute the outermost capturing group, which in this case should contain /1 instead of 1. By making it a non-capturing group, Django will substitute the argument 1 into the inner group instead of the outer group.
So i have a main urls.py that looks like this:
urlpatterns = patterns('',
(r'^users/(?P<username>.*)/', include('app.urls')),
url(r'^users/(?P<username>.*)$',direct_to_template,{'template':'accounts/profile.html'}, name='profile'),)
and a app.urls.py
urlpatterns = patterns('',url(r'^app/create/$', create_city ,name='create_city'),)
my problem is, when i localhost:8000/users/michael/app/create/ it doesn't call my view. I have tried changing the order of the urls with no luck so i believe my problem is with the regular expressions but with no idea on what to change, anyone?
The named group (?P<username>.*) will match any characters, zero or more times. in your username, including forward slashes.
In url patterns, it would be more common to use (?P<username>[-\w]+). This would match at least one character from the set of lowercase a-z, uppercase A-Z, digits 0-9 hyphens and underscores.
I also recommend that you add a trailing slash to your pattern for your profile view.
Putting it all together, I suggest you use the following as a starting point for your urls.py:
urlpatterns = patterns('',
url(r'^users/(?P<username>[-\w]+)/$',direct_to_template, {'template':'accounts/profile.html'}, name='profile'),
(r'^users/(?P<username>[-\w]+)/', include('app.urls')),
)