django-urls

Django urls.py and views.py behaviour -

♀尐吖头ヾ 提交于 2019-12-06 19:32:31
I'm currently experimenting with Django and creating apps following the tutorials on the official website. So my urls.py looks like: urlpatterns = patterns('', (r'^/$','ulogin.views.index'), #why doesn't this work? (r'^ucode/$', 'ulogin.views.index'), (r'^ucode/(\d+)/$', 'ulogin.views.index'), ) And my views.py looks like: def index(request): return HttpResponse("Hello, world. You're at the poll index.") def redirect_to_index(request): return HttpResponseRedirect('/ucode/') When I run the server to check the test url, http://127.0.0.1:8000/ucode displays "Hello, world...etc" correctly, and

Raise 404 and continue the URL chain

匆匆过客 提交于 2019-12-06 16:36:51
问题 I've got a URLs pattern like this: urlpatterns = ( url(r'^$', list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/$', show_title, name='title'), ) The filtered-list and title match the same things. If there is an available list of things matching the tag in filtered-list , I want list_titles to fire off. But if there isn't a matching tag , I want to bubble that back to the URL processor so show_title fires off. If there's no

Is there any way that I can write URL in django using annotations in python

半城伤御伤魂 提交于 2019-12-06 16:01:46
问题 I am from the Java Hibernate and Symfony2 background where I used to write the routing within the controller on the top of function like this: /** * @Route("/blog") */ class PostController extends Controller { I know its not available in Django but is there any way I can code some decorator etc. so that I can mention the URL like this: @URL("/mytest") class myView(): pass 回答1: While it would be very undjangonic , you could try something like this: project/ decorators.py views.py urls.py #

Using both sort & filter on a QuerySet

和自甴很熟 提交于 2019-12-06 14:58:27
I have a list of userprofiles that I want to be able to sort and filter. I have been able to do this manually, by manually typing in the URLs, but I haven't been able to code the template page to allow the persistence of a previously-applied filter or sort. Here is the url and template code that I currently have -- # in urls url(r'^talent/filter\:(?P<position>[A-Za-z]+)/sort\:(?P<sort>[A-Za-z]+)$', 'talent_filter', name='talent_filter_sort'), url(r'^talent/filter\:(?P<position>[A-Za-z]+)/$', 'talent_filter', name='talent_filter'), url(r'^talent/sort\:(?P<sort>[A-Za-z]+)/$', 'talent_sort', name

a way to have django SITEURL constant in settings.py

眉间皱痕 提交于 2019-12-06 13:16:25
I am from PHP background, and I know that constants can be accessed at most of places in a framework and I think it is same in django as well. I tried to have that URL too in django but I tried to have it from django.contrib. I tried to utilize django's Site class and imported that. But the problem is that at time of loading settings.py I can't import any django contrib. file. So how can I have SITE URL automatically that I can use anywhere, in template as well as at other places.What is the best way to do so? Do any python utility can do so? Whatever you define in your settings.py, for

Django slug url in perisan 404

安稳与你 提交于 2019-12-06 09:44:26
I have a django url: path('question/<slug:question_slug>/add_vote/', views.AddVoteQuestionView.as_view()) It work fine with english slug but when slug is persian something like this: /question/سوال-تست/add_vote/ django url throw 404 Not Found , is there any solution to catch this perisan slug url? EDIT: I'm using django 2.1.5. It work fine with this url: re_path(r'question/(?P<question_slug>[\w-]+)/add_vote/$', views.AddVoteQuestionView.as_view()) This is an addition to Selcuk answer given here to pass such language/unicode characters you have to Write some custom path converter Use re_path()

detect the HOST domain name in django models

不羁的心 提交于 2019-12-05 21:38:34
In my model, I want to use the domain name (HOST) I'm using in my views. In views that'd be doable, thanks to the "request" object. But how do I do this models methods? Which don't use "HttpRequest" objects? Now I'm setting a global value HOST in settings.py and using it, but that's ugly. Also, I don't really want to manage "Sites" (the Sites app) — Is there a way, I can grab the "by default" Site Host name? Thanks a lot for your help! (and sorry for my poor English) If you're calling the model method from a view, you could add a parameter for the request to the model method and include it

Passing arguments to views in Django from constrained choices

送分小仙女□ 提交于 2019-12-05 18:50:40
I am looking for the best way to pass either of two arguments to the views from the URL, without allowing any additional arguments. For example, with the following URLs: (r'^friends/requests', 'app.views.pendingFriends'), (r'^friends/offers', 'app.views.pendingFriends'), If it's possible to pass the URL to the views, so that pendingFriends knows which URL it was called from, that would work. However, I can't see a way to do this. Instead, I could supply the arguments ( requests or offers ) in the URL, to a single Django view, (r'^friends/(?P<type>\w+', 'app.views.pendingFriends'), The argument

How to add namespace url to a django-rest-framework router viewset

时光毁灭记忆、已成空白 提交于 2019-12-05 16:14:52
问题 I would like to add a url namespace to my api router but when I do the router still looks for urls without a namespace: router = DefaultRouter() router.register(r'users', UserViewSet) router.register(r'events', EventViewSet) router.register(r'comments', CommentViewSet) urlpatterns = patterns('apiroot.views', url(r'^', include(router.urls, namespace='api')), ) The browsable api looks for url names like 'user-list' and 'user-detail' still instead of 'api:user-list' which is what I would like to

Outputing text from urls.py in Django

扶醉桌前 提交于 2019-12-05 14:23:17
Is there any way I can simply output text (not HTML) from urls.py instead of calling a function (in views.py or otherwise)? urlpatterns = patterns('', url(r'^$', "Hello World"), ) No, definitly. A url must map a pattern to a callable, and this callable must accept a HttpRequest as first argument and return a HttpResponse . The closer thing you could do would be to map the pattern to a lambda (anonymous function), ie: from django.http import HttpResponse urlpatterns = patterns('', url(r'^$', lambda request: HttpResponse("Hello World", content_type="text/plain")), ) but I would definitly not