django-urls

Django templates folders

匆匆过客 提交于 2019-11-27 11:29:13
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 the template in blog/templates/... Instead of the templates folder in my root. Which contains the base

Using {% url ??? %} in django templates

不问归期 提交于 2019-11-27 09:28:10
问题 I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every permutation possible and have resorted to posting here as a last resort. So here it is. My urls.py looks like this: from django.conf.urls.defaults import * from login.views import * from mainapp.views import * import settings # Uncomment the next two

Any /polls URL always call index() function in views.py

匆匆过客 提交于 2019-11-27 08:48:15
问题 polls/urls/py from django.conf.urls import url from . import views urlpatterns = [ url('', views.index, name='index'), url('<int:question_id>/', views.detail, name='detail'), # ex: /polls/5/results/ url('<int:question_id>/results/', views.results, name='results'), # ex: /polls/5/vote/ url('<int:question_id>/vote/', views.vote, name='vote'), ] views.py from __future__ import unicode_literals from django.http import HttpResponse from .models import Question from django.template import loader #

Django URL Redirect

纵饮孤独 提交于 2019-11-27 06:51:20
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 . You can try the Class Based View called RedirectView from django.views.generic.base import RedirectView urlpatterns

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

梦想的初衷 提交于 2019-11-27 06:35:59
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 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, you can use the built-in django.core.context_processors.request context processor, and then use {{ request

django urls without a trailing slash do not redirect

℡╲_俬逩灬. 提交于 2019-11-27 06:24:18
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 on computer A. Is there something I missed? Both url.py files look identical to me. check your APPEND

Linking to the django admin site

柔情痞子 提交于 2019-11-27 05:35:14
问题 Very basic question, but I'm having trouble tracking down the answer on the web. I have a template, which I want to link to the django admin site (i.e. localhost:8000/admin). What is the code for this? I'm imagining something like <a href="{% url admin.site.root %}">link to admin panel</a> However, when I try the above snippet I get: Caught an exception while rendering: Reverse for 'project_name.django.contrib.admin.sites.root' with arguments '()' and keyword arguments '{}' not found. Help?

How do I redirect in Django with context?

大憨熊 提交于 2019-11-27 04:22:14
问题 I have a view that validates and saves a form. After the form is saved, I'd like redirect back to a list_object view with a success message "form for customer xyz was successfully updated..." HttpResponseRedirect doesn't seem like it would work, because it only has an argument for the url, no way to pass dictionary with it. I've tried modifying my wrapper for object_list to take a dict as a parameter that has the necessary context. I the return a call to this wrapper from inside the view that

Django and service workers - serve “sw.js” at application's root url

冷暖自知 提交于 2019-11-27 03:25:22
问题 So I'm building a Django progressive web app with offline support using service workers. According to google's documentation, the sw.js file should be at the root of the app's url: You need to do this because the scope of a service worker (the set of urls that the ServiceWorker will load for) is defined by the directory where it resides. At the moment, I'm serving all static assets from http://example.com/static/ folder. But I need to serve this specific file at a url like: http://example.com

Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'

不羁的心 提交于 2019-11-27 01:44:54
问题 I'm new to Django and am trying to create the back end code for a music application on my website. I have created the correct view in my views.py file (in the correct directory) as shown below: def detail(request, album_id): return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>") however, when creating the url or path for this (shown below) #/music/71/ (pk) path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'), I am experiencing a warning on my terminal stating: ?: