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.
Related
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.
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.
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.+)/'.
I'm not sure if it's very specific but I think what I am going to ask is being used in various places in different contexts. My problem is mainly related to URL patterns that I've created for my Django application. I need a profile URL for all application users, so I am creating a URL pattern as below.
urlpatterns = patterns('apps.user_profile.views',
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
url(r"^app/$", 'app', name="app_page"),
)
As is very clear, I am mapping URLs with user names in the path to fetch user data dynamically. This is working fine, but the problem comes when the system gets a request for app page. In this case, the request goes to user profile, since it accepts all kinds of words and is ordered before app view in urls.py.
Question :
Is there any way to specify, where a request is not resolved in user_profile view, to continue looking at other URLs in the urls.py of app?
Patterns are matched in the order they are written, so simply moving your app pattern before your username pattern will solve your problem:
urlpatterns = patterns('apps.user_profile.views',
url(r"^app/$", 'app', name="app_page"),
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
)
Now, only if a url doesn't match app/ it will be sent to your user profile view.
Of course, you will have an issue the day a user signs up with the username of app.
I'm trying to use one app to satisfy multiple url paths. That is to say, I want the url /blog/ and /job/ to use the same app, but different views. There are a number of ways to do this I'm sure, but none of them seem very clean. Here's what I'm doing right now
# /urls.py
urlpatterns = patterns("",
(r"^(blog|job)/", include("myproject.myapp.urls")),
)
# /myapp/urls.py
urlpatterns = patterns("myproject.myapp.views",
(r"^(?P<id>\d+)/edit/$", "myproject.myapp.views.edit"),
(r"^(?P<id>\d+)/delete/$", "myproject.myapp.views.delete"),
(r"^(?P<id>\d+)/update/$", "myproject.myapp.views.update"),
(r"^insert/$", "myproject.myapp.views.insert"),
)
urlpatterns += patterns("",
(r"^(?P<object_id>\d+)/$", "django.views.generic.list_detail.object_detail", info_dict, "NOIDEA-detail"),
(r"^/$", "django.views.generic.list_detail.object_list", info_dict, "NOIDEA-community"),
)
# /myapp/views.py
def edit(request, type, id):
if (type == "blog"):
editBlog(request, id)
else (type == "job")
editJob(request, id)
def editBlog(request, id):
# some code
def editJob(request, id):
# some code
I've ended up breaking all of this into multiple model and view files to make the code cleaner but the above example doesn't account for things like reverse url lookups which breaks all of my template {% url %} calls.
Originally, I had blogs, jobs, events, contests, etc all living in their own apps, but all of their functionality is so similar, that it didn't make sense to leave it that way, so I attempted to combine them... and this happened. You see those "NOIDEA-detail" and "NOIDEA-community" url names on my generic views? Yeah, I don't know what to use there :-(
You can have more than one modules defining URLs. You can have /blog/ URLs in myapp/urls.py and /job/ URLs in myapp/job_urls.py. Or you can have two modules within a urls subpackage.
Alternatively you can manually prefix your url definitions:
urlpatterns = patterns("myproject.myapp.views",
(r"^jobs/(?P<id>\d+)/edit/$", "myproject.myapp.views.edit"),
(r"^jobs/(?P<id>\d+)/delete/$", "myproject.myapp.views.delete"),
(r"^jobs/(?P<id>\d+)/update/$", "myproject.myapp.views.update"),
(r"^jobs/insert/$", "myproject.myapp.views.insert"),
)
urlpatterns += patterns("",
(r"^blog/(?P<object_id>\d+)/$", "django.views.generic.list_detail.object_detail", info_dict, "NOIDEA-detail"),
(r"^blog/$", "django.views.generic.list_detail.object_list", info_dict, "NOIDEA-community"),
)
And then mount them as:
urlpatterns = patterns("",
(r"", include("myapp.urls")),
)
Personally I would go for more RESTful URL definitions though. Such as blog/(?P<post_id>\d+)/edit/$.
Looks pretty good to me. If you want reverse lookups, just have a different reverse name for each url format, even if they end up pointing to the same view.