django-urls

Django: Multiple url patterns starting at the root spread across files

戏子无情 提交于 2019-11-30 11:16:35
I am wondering if it is possible to have the standard url patterns spread across multiple files (in this case the project-wide urls.py and several apps-specific urls.py ). Imagine that the project urls.py look like this (got this working): from django.conf.urls import patterns, include, url admin.autodiscover() urlpatterns = patterns('', url(r'^user/signup/', 'registration.views.signup'), url(r'^user/confirm/(?P<code>\w{20})/', 'registration.views.confirm'), url(r'^user/profile/(\d+)/', 'profile.views.show'), url(r'^user/profile/edit/', 'profile.views.edit'), ) As you can see, I have two

Django: Overwrite ROOT_URLCONF with request.urlconf in middleware

痞子三分冷 提交于 2019-11-30 10:06:31
I am trying to overwrite ROOT_URLCONF with another url when the request contains "api" subdomain and this is what I have so far. from django.utils.cache import patch_vary_headers class SubdomainMiddleware: def process_request(self, request): path = request.get_full_path() root_url = path.split('/')[1] domain_parts = request.get_host().split('.') if (len(domain_parts) > 2): subdomain = domain_parts[0] if (subdomain.lower() == 'www'): subdomain = None else: subdomain = None request.subdomain = subdomain request.domain = domain if request.subdomain == "api": request.urlconf = "rest_api_example

Django: Arbitrary number of unnamed urls.py parameters

旧时模样 提交于 2019-11-30 08:17:05
I have a Django model with a large number of fields and 20000+ table rows. To facilitate human readable URLs and the ability to break down the large list into arbitrary sublists, I would like to have a URL that looks like this: /browse/<name1>/<value1>/<name2>/<value2>/ .... etc .... where 'name' maps to a model attribute and 'value' is the search criteria for that attribute. Each "name" will be treated like a category to return subsets of the model instances where the categories match. Now, this could be handled with GET parameters, but I prefer more readable URLs for both the user's sake and

Pass url argument to ListView queryset

本秂侑毒 提交于 2019-11-30 06:52:27
models.py class Lab(Model): acronym = CharField(max_length=10) class Message(Model): lab = ForeignKey(Lab) urls.py urlpatterns = patterns('', url(r'^(?P<lab>\w+)/$', ListView.as_view( queryset=Message.objects.filter(lab__acronym='') )), ) I want to pass the lab keyword argument to the ListView queryset. That means if lab equals to TEST , the resulting queryset will be Message.objects.filter(lab__acronym='TEST') . How can I do that? You need to write your own view for that and then just override the get_queryset method: class CustomListView(ListView): def get_queryset(self): return Message

How to access url hash/fragment from a Django Request object

六眼飞鱼酱① 提交于 2019-11-30 06:00:54
As in the title: how can I access the url hash/fragment (the part following the dash # ) from a Django view and so, I suppose, from a Django Request object? I've not found enough information on the documentation here available: http://docs.djangoproject.com/en/dev/ref/request-response/ P.S. Suppose that the fragment part is sent to the server (it is so in my specific case since it's not a browser to send the request). This is not sent to the server, by definition. From URI References: Fragment Identifiers on URIs : "The HTTP engine cannot make any assumptions about it. The server is not even

How to redirect url pattern with variables from urls.py in Django?

混江龙づ霸主 提交于 2019-11-30 03:42:47
问题 I'd like to redirect url pattern with variables from urls.py . I refer other stackoverflow solution, but I don't know when url having a variable like following code. from django.conf.urls import patterns, url from django.views.generic import RedirectView urlpatterns = patterns( url( r'^permalink/(?P<id>\d+)/foo/$', RedirectView.as_view(url='/permalink/(?P<id>\d+)/') ), ) With this code, django will redirect /permalink/1/foo/ to /permalink/(?P<id>\d+)/ , not the /permalink/1/ . Is there any

Django AttributeError 'tuple' object has no attribute 'regex'

风格不统一 提交于 2019-11-30 00:46:04
问题 I'm using django 1.8 and having trouble with it. I am trying to import tinymce in my project. When I render it caught AttributeError: tuple' object has no attribute 'regex' When I remove the url in url.py it is working. Here is my codes. url.py from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ # Examples: # url(r'^$', 'hizlinot.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), (r'^tinymce/',

Django: What is the difference b/w HttpResponse vs HttpResponseRedirect vs render_to_response

独自空忆成欢 提交于 2019-11-29 21:54:10
The above mentioned things are giving me almost the same results was wondering whats the main difference in them. Ofri Raviv response = HttpResponse("Here's the text of the Web page.") : will create a new HttpResponse object with HTTP code 200 (OK), and the content passed to the constructor. In general, you should only use this for really small responses (like an AJAX form return value, if its really simple - just a number or so). HttpResponseRedirect("http://example.com/") : will create a new HttpResponse object with HTTP code 302 (Found/Moved temporarily). This should be used only to

Anyone knows good Django URL namespaces tutorial? [closed]

懵懂的女人 提交于 2019-11-29 19:53:22
I'm looking for a good tutorial for URL namespaces in Django. I find official documentation a little too sparse - it lacks good examples. I found similar question here on stack, but the answers didn't help me to fully understand the subject either. David Eyk Agreed, the docs for this are rather confusing. Here's my reading of it (NB: all code is untested! ): In apps.help.urls : urlpatterns = [ url(r'^$', 'apps.help.views.index', name='index'), ] In your main urls.py : urlpatterns = [ url(r'^help/', include('apps.help.urls', namespace='help', app_name='help')), url(r'^ineedhelp/', include('apps

Django: Is there a better way to bold the current page link

早过忘川 提交于 2019-11-29 19:22:27
问题 I have a base.html template that contains a list of links. Example: <div id="sidebar1"> <ul> <li><a href="/" title="">Index</a></li> <li><a href="/stuff/" title="" class="current">Stuff</a></li> <li><a href="/about/" title="">About Me</a></li> <li><a href="/contact/" title="">Contact Me</a></li> </div> Then I have in my views.py a definition for each of index.html, stuff.html, about.html and contact.html. Each of those templates simply derive from a base.html template and set their own