I have created the following urlpatters
urlpatterns=[
url(r'(?P<user_name>[a-zA-Z]+)/$', views.profile_view,
kwargs=None, name='userprofile'),
url(r'(?P<user_name>[a-zA-Z]+)/interests/$',views.interest,name='interests')
]
But when I enter the url localhost:8000/mainuser/interest/ it treat it as first url and opens profile_view. It is clearly matching '/'. Help me with this.
You should begin your URL patterns with the start of line character ^. Because you are not including this character the regex for the first pattern matches any URL that ends with 1 or more characters followed by a forward-slash.
urlpatterns=[
url(r'^(?P<user_name>[a-zA-Z]+)/$', views.profile_view, kwargs=None, name='userprofile'),
url(r'^(?P<user_name>[a-zAZ]+)/interests/$',views.interest,name='interests'),
]
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),
I want to match the following url
/calendar/entry/add/2013/11/23/05:30
my urls.py
url(r'^entry/add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<time>\d+:\d+)/$', 'view')
and on main urls.py I have
url(r'^calendar/',include('mycal.urls'),
but it isnt working...and I get a no match. What is the right regexpr for this....
The url /calendar/entry/add/2013/11/23/05:30 does not contain trailing /.
Remove the trailing / from the url pattern or make it optional to make it match with the url
url(r'^entry/add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<time>\d+:\d+)/?$', 'view')
# ^^
Error can be seen here: http://djaffry.selfip.com:8080/
I want the index page to accept parameters, whether it be
mysite.com/search/param_here
or
mysite.com/?search=param_here
I have this in my URL patterns, but I can't get it to work. Any suggestions?
urlpatterns = patterns('',
(r'^$/(?P<tag>\w+)', 'twingle.search.views.index'),
)
First of all your regular expression in url pattern is wrong.
r'^$/(?P<tag>\w+)'
It says to match everything from
^ the beginning of line
$ to the end of line
having pattern named tag which is composed of words and digits after the line end
Usually after the one line ends comes another line or EOF not content (unless you use multiline regexp and you don't need those here).
Line end should be after the tag:
r'^/(?P<tag>\w+)$'
Using a query string
Query strings are not parsed by url reslover.
Thus, if you have url in format:
http://mysite.com/?query=param_here
will match:
(r'^$', 'twingle.search.views.index')
In this case you can access query string in view like so:
request.GET.get('query', '')
Without a query string
mysite.com/search/param_here
will match:
(r'^search/(?P<query>\w+)$', 'twingle.search.views.index'),
Where everything that matches \w (you should change this to suite your needs) will be passed along with request to index view function as argument named query.
Both
You can use both url patterns like so:
urlpatterns = patterns('twingle.search.views',
url(r'^$', 'index'),
url(r'^search/(?P<query>\w+)$', 'index'),
)
In this example the view would look something like this:
def index(request, query=None)
if not query:
query = request.GET.get('query', '')
# do stuff with `query` string