Django Reserved Url Names - python

I have these views in my django website. I want to redirect my users to categories like
http://sitename.com/category1/
http://sitename.com/category2/
http://sitename.com/category3/
but django detects my view names like category names if i want to go watch page or register page like:
http://sitename.com/register/
http://sitename.com/watch/
django redirects me to category view. How can i fix my problem?
url(r'^management/', include(admin.site.urls)),
url(r'^$', views.ana_sayfa),
url(r'^(.+)/', views.kategori),
url(r'^register/', views.kayit_sayfasi),
url(r'^watch/(.+)/', views.ondemand_izleme_sayfasi),
url(r'^event/(.+)/', views.live_stream_sayfasi),
url(r'^live/(.+)/', views.live_stream_izleme_sayfasi),
url(r'^buy/(.+)/', views.live_stream_satin_alma_sayfasi),
url(r'^search/(.+)/', views.arama),
url(r'^manager/', views.video_yoneticisi),
url(r'^lists/', views.listelerim),
url(r'^profile/', views.bilgilerimi_guncelle),
url(r'^messages/', views.mesajlarim),
url(r'^subscriptions/', views.abonelikler),
url(r'^settings/', views.bildirim_ayarlari),
url(r'^contact/', views.iletisim),
url(r'^help/', views.yardim),
url(r'^rss/', views.rss),
url(r'^oneall/', include('django_oneall.urls')),

Your category url pattern is evaluated before the other patterns.
You could move it to the bottom, so all of the others will be evaluated first.
So move this line to the bottom:
url(r'^(.+)/', views.kategori),
See also URL dispatching:
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

The URL routing patterns are evaluated in order. You need to either move your category route url(r'^(.+)/', views.kategori), down to the bottom, as ^(.+) matches everything with one or more letters plus a slash, or change the regular expression from '^(.+)/' to something like '^(category.+)/'.

Related

Django url re_path failed to redirect to the correct view

Django re_path did not match and I don't know the reason why.
urls.py
urlpatterns = [
..
re_path(r'^localDeliveryPayment\?paymentId\=(?P<UUID>[0-9-a-z]{32})$', verifyMobilePayReceived.as_view()),
re_path(r'^localDeliveryPayment$', Payment.as_view(), name = 'localDeliveryPayment'),
..
]
If url
www.example.com/localDeliveryPayment the user is directed to Payment view.
If url www.example.com/localDeliveryPayment?paymentId=00340000610febab0891e9008816d3e9 the user should be directed to verifyMobilePayReceived view. The problem is that right now www.example.com/localDeliveryPayment?paymentId=00340000610febab0891e9008816d3e9 is still directed to Payment view.
You are trying to capture a GET parameter in your URL routing which is the incorrect way of doing what you are trying to do.
Either continue with your current method of passing a paymentId GET parameter and check for its presence in you Payment.as_view() view OR you can rework this to something along the lines of.
re_path(r'^localDeliveryPayment/<UUID:NAME_OF_UUID_IN_MODEL>/', verifyMobilePayReceived.as_view()),
This should provide the filtering for the model you desire.

URL pattern in django to match limited set of words

I have a URL pattern in Django where there is a variable (called name) that should only take one of a select list of words. Something like this:
path("profile/<marta|jose|felipe|manuela:name>/", views.profile),
So basically only these paths are valid:
profile/marta/
profile/jose/
profile/felipe/
profile/manuela/
How exactly can I configure this in the Django url patterns? The above syntax tries to illustrate the idea but doesn't work. I've seen this ten year old question but that uses the previous format in the url patterns file so I imagine it should be done differently now...?
Why not put them in view?
from django.http import Http404
def profiles(request, name):
if not name in ['marta', 'jose', 'felipe', 'manuela']:
raise Http404('You shall not pass!')
Or you can simply use re_path:
urlpatterns = [
re_path(r'^profile/(?P<name>marta|jose|felipe|manuela)/$', views.index, name='index'),
]

Can you have two of the same regex but point to them to different url files

For example is the following allowed? If so, is it not recommended or is it okay?
urlpatterns = [
url(r'^$', include('login.urls')),
url(r'^$' include('register.urls')),
url(r'^admin/', admin.site.urls),
]
Yes, you can set it up in django, but the second one will not be used, because django will find the url from top to bottom, when it found the match record, django will return the first record and stop there, so, the second one could not have chance to execute.
No, Django will math the first regex.
But you can for example set one regex for one view
and than in that view do specific operations based on the type of the request (GET/POST/PUT etc)
class CommentView(View):
def get(self, request):
... do if get type
def post(self, request):
... do if post type
And also you can check in view if user is logged in or not, if not you can redirect them to login.

Incorrect Django URL pattern match for 2 views with the same URL structure?

I have two url patterns in Django:
urlpatterns += patterns('',
url(r'^(?P<song_name>.+)-(?P<dj_slug>.+)-(?P<song_id>.+)/$', songs.dj_song, name='dj_song'),
url(r'^(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$', songs.trending_song, name='trending_song'),
)
When I visit a URL of the first pattern, it opens it correctly. However if I try and visit a URL of the second pattern, it tries to access the first view again. The variables song_name, dj_slug, artist_slugare strings and song_id is an integer.
What should be the URL patterns for such a case with similar URL structure?
Both urls use the same regex. I removed the group names and get:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
You should use different urls for different views. For example add the prefix to the second url:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),

Django Url Conf Dynamic Url Conflict

So I have two models in the same app that have pretty much identical url structures:
urlpatterns = patterns('',
#....
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
url(r'^(?P<company>[\w-]+)/(?P<slug>[\w-]+)/$', 'PrizeItem_Index', name="prizepage"),
))),
# Old Redirects
)
The problems here being Reviews and PrizeType. I want my urls structured so that a user looking for prizes under a certain category goes to /prizes/prizetype. But if they want to see prizes under a certain company, then they'd go to /prizes/companyslug/. However, these two urls will naturally conflict. I can always just change the url structure, though I'd rather not. I just want to know if there are any ways around this that don't involve changing the url structure.
I would suggest writing a helper view function, which checks whether the inputted url corresponds to a company or a category, and then redirecting the request to the appropriate page.
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', prizehelper, name="prizehelper),
where, you can check within prizehelper, if it is a company or a category and move on accordingly.
Another approach could be, to change your url structure, and reflect which type of url it is
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^company/(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^category/(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
Have a single urlconf entry that goes to a view which figures out which type is being examined and then dispatches to the appropriate view for that type.

Categories