django-urls

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

眉间皱痕 提交于 2019-11-26 18:37:24
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 username section of the address changes for every user. Any ideas how I can make it so when the user is

Django optional url parameters

梦想的初衷 提交于 2019-11-26 18:13:26
I have a Django URL like this: url( r'^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$', 'tool.views.ProjectConfig', name='project_config' ), views.py: def ProjectConfig(request, product, project_id=None, template_name='project.html'): ... # do stuff The problem is that I want the project_id parameter to be optional. I want /project_config/ and /project_config/12345abdce/ to be equally valid URL patterns, so that if project_id is passed, then I can use it. As it stands at the moment, I get a 404 when I access the URL without the project_id parameter. Yuji 'Tomita' Tomita There are

Django multi tenancy

折月煮酒 提交于 2019-11-26 17:58:29
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 {% url .. %} ... It seems to be returning /x/company/ as a default match for the regex. since the

django urls without a trailing slash do not redirect

痞子三分冷 提交于 2019-11-26 17:57:54
问题 I've got two applications located on two separate computers. On computer A, in the urls.py file I have a line like the following: (r'^cast/$', 'mySite.simulate.views.cast') And that url will work for both mySite.com/cast/ and mySite.com/cast . But on computer B I have a similar url written out like: (r'^login/$', 'mySite.myUser.views.login') For some reason on computer B the url mySite.com/login / will work but mySite.com/login will hang and won't direct back to mySite.com/login/ like it will

Django URL Redirect

邮差的信 提交于 2019-11-26 17:35:38
问题 How can I redirect traffic that doesn't match any of my other URLs back to the home page? urls.py: urlpatterns = patterns('', url(r'^$', 'macmonster.views.home'), #url(r'^macmon_home$', 'macmonster.views.home'), url(r'^macmon_output/$', 'macmonster.views.output'), url(r'^macmon_about/$', 'macmonster.views.about'), url(r'^.*$', 'macmonster.views.home'), ) As it stands, the last entry sends all "other" traffic to the home page but I want to redirect via either an HTTP 301 or 302 . 回答1: You can

Django templates folders

戏子无情 提交于 2019-11-26 15:35:48
问题 I'm experimenting with Django, and figuring out how to set urls.py , and how the URLs work. I've configured urls.py in the root of the project, to directs to my blog and admin. But now I want to add a page to my home, so at localhost:8000 . So I've added to following code to the urls.py in the root of the project: from django.views.generic.simple import direct_to_template urlpatterns = patterns('', (r"^$", direct_to_template, {"template": "base.html"}), ) The problem is that it searches for

Django: get URL of current page, including parameters, in a template

北城以北 提交于 2019-11-26 12:06:26
问题 Is there a way to get the current page URL and all its parameters in a Django template? For example, a templatetag that would print a full URL like /foo/bar?param=1&baz=2 回答1: Write a custom context processor. e.g. def get_current_path(request): return { 'current_path': request.get_full_path() } add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS settings variable, and use it in your template like so: {{ current_path }} If you want to have the full request object in every request,

Django : How can I see a list of urlpatterns?

柔情痞子 提交于 2019-11-26 08:59:42
问题 How can I see the current urlpatterns that \"reverse\" is looking in? I\'m calling reverse in a view with an argument that I think should work, but doesn\'t. Any way I can check what\'s there and why my pattern isn\'t? 回答1: If you want a list of all the urls in your project, first you need to install django-extensions, add it to your settings like this: INSTALLED_APPS = ( ... 'django_extensions', ... ) And then, run this command in your terminal ./manage.py show_urls For more information you

Is it better to use path() or url() in urls.py for django 2.0?

主宰稳场 提交于 2019-11-26 08:49:03
问题 In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I\'ve seen other examples on youtube of this. e.g. from django.contrib import admin from django.urls import include from django.conf.urls import url urlpatterns = [ path(\'admin/\', admin.site.urls), url(r\'^polls/\', include(\'polls.urls\')), ] #and in polls/urls.py urlpatterns = [ url(r\'^$\', views.index, name=\"index\"), ] However, in going through

Difference between static STATIC_URL and STATIC_ROOT on Django

谁说我不能喝 提交于 2019-11-26 07:55:46
问题 I am confused by static root and want to clarify things. To serve static files in Django, the following should be in settings.py and urls.py : import os PROJECT_DIR=os.path.dirname(__file__) 1. Absolute path to the directory in which static files should be collected STATIC_ROOT= os.path.join(PROJECT_DIR,\'static_media/\') 2. URL prefix for static files STATIC_URL = \'/static/\' 3. Additional locations for static files STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,\'static/\'),) ...and in urls