django-urls

Facing problem with user profile url scheme like example.com/username in django

百般思念 提交于 2019-12-19 10:41:44
问题 In a django app, I need to create twitter user profile urls with following structure like: example.com/<username> example.com/<username>/friends example.com/<username>/blog example.com/<username>/some-page example.com/<username>/some-other-page My urls.py: urlpatterns = patterns('profiles.views', url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/$', 'show_profile', name='profiles_show_profile'), url(r'^(?P<account_name>[a-zA-Z0-0_.-]+)/friends/$', 'show_friends', name='profiles_show_blog'), url(r'^(?P

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

天大地大妈咪最大 提交于 2019-12-18 14:18:27
问题 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+)/',

Django: Overwrite ROOT_URLCONF with request.urlconf in middleware

青春壹個敷衍的年華 提交于 2019-12-18 13:35:29
问题 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 =

How to access HttpRequest from urls.py in Django

▼魔方 西西 提交于 2019-12-18 13:30:47
问题 Basically I want to use a generic view that lists objects based on a username. Now, the question is, how do I do something like: (r'^resources/$', ListView.as_view( queryset=Resources.objects.filter(user=request.user.username), ... ) ) I couldn't find a way to access the HttpRequest (request) object though... Or do I need to use my own views and do all object selection there? 回答1: If you really want to clutter your URLconf directly, you can do it like so: (r'^resources/$', lambda request:

Django 404 error-page not found

a 夏天 提交于 2019-12-18 04:11:55
问题 My project is named homefood, and when I runserver I get this error.Anybody have any clue how to fix this error. Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ Using the URLconf defined in homefood.urls, Django tried these URL patterns, in this order: ^foodPosts/ ^admin/ The current URL, , didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404

Django UpdateView without pk in url

不问归期 提交于 2019-12-17 18:38:39
问题 Is it possible eliminate pk from url related to UpdateView ? For example, if I have url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update") is there any way to write it like url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update") and then send pk as a parameter in POST or GET request? 回答1: Yes it is possible you just need to override the get_object method: from django.views.generic.edit import UpdateView class UpdateMyObj(UpdateView): # ..... def get_object

How to identify an anchor in a url in Django?

北城余情 提交于 2019-12-17 17:14:25
问题 I'm doing a slideshow and each slide has a url format like this: articles/1234#slide=5 . I want to retrive the slide=5 part from the url in my url.py file and then pass it to the corresponding view function and finally, pass it to the template and render the right slide. The url settings is as follows: url(r'^(?P<article_id>\d+)#slide=(?P<current_slide>\d{1,2})$', 'articles.views.show_article') But it seems that it cannot get the current_slide variable from the url. I guess it has something

How to handle request.GET with multiple variables for the same parameter in Django

吃可爱长大的小学妹 提交于 2019-12-17 06:31:28
问题 In a Django view you can access the request.GET['variablename'] , so in your view you can do something like this: myvar = request.GET['myvar'] The actual request.GET['myvar'] object type is: <class 'django.http.QueryDict'> Now, if you want to pass multiple variables with the same parameter name, i.e: http://example.com/blah/?myvar=123&myvar=567 You would like a python list returned for the parameter myvar , then do something like this: for var in request.GET['myvar']: print(var) However, when

Django - after login, redirect user to his custom page --> mysite.com/username

有些话、适合烂在心里 提交于 2019-12-17 04:20:14
问题 By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py. This is great but I would like the user (after login) to be redirected to a custom page where the link to that page would look something like this: mysite.com/username . So the default accounts/profile or the LOGIN_REDIRECT_URL settings would not work in this case since both are somehow static. In my case the

Django multi tenancy

 ̄綄美尐妖づ 提交于 2019-12-17 04:07:49
问题 Tl; dr: Is there a way to override the default behaviour of reverse? In my django project I have alot of urls such as url(r'^\w+/company/', include("company.urls", namespace="company")), Which allows for urls such as .../companyA/company/ .../companyB/company/ So that I can then use a custom middleware to modify the request to include some specific details based upon what company is using my site This all works fine except for when django is trying to decipher the full path with reverse and {