我正在尝试使用Django开发一个感谢信系统。我已经设置了admin ok,并且能够通过它向数据库中添加新条目。我还能够设置索引,显示数据库中所有当前的感谢信,还有一个详细视图,显示给定感谢信的完整详细信息。我正在尝试创建一个表单视图,让没有管理员权限的人可以向系统添加感谢信。表单加载正常,字段可以填充,但每次按提交我都会收到404错误。唯一没有发生这种情况的方法是,如果我没有设置表单操作:
<form id=“new_ty_form” method=“post” action=>
并按下提交只会重新生成一个空白表单,并且不会对之前的字段执行任何操作。
我也完全无法实现{%url%}系统,这意味着我必须对我正在使用的所有网址进行硬编码。每当我尝试使用{%url%}时,我都会收到以下错误:
TemplateSyntaxError at /thank_you/
Could not parse the remainder: '‘thank_you.views.detail' from '‘thank_you.views.detail'. The syntax of 'url' changed in Django 1.5, see the docs.
我完全坚持这个,请有人帮忙吗?我将尽可能多地包含我的代码。
的mysite / settings.py
"""
Django settings for mysite project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '13k@ki=_o)(e$&8015fs4y#q=!3==92$6^dzn(iuu_h4&n6&d)'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'thank_you',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
的mysite / urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^thank_you/', include('thank_you.urls')),
url(r'^admin/', include(admin.site.urls)),
)
thank_you / models.py
from django.db import models
class ThankYou(models.Model):
MODERATION_STATUS = (
('unmoderated', 'Unmoderated'),
('accepted', 'Accepted'),
('rejected', 'Rejected'),
)
mod_status = models.CharField(max_length=12, choices=MODERATION_STATUS, default='unmoderated')
your_name = models.CharField('Senders Name', max_length=50)
fb_rec = models.CharField('Recipients Name', max_length=50)
rec_dep = models.CharField('Recipients Department',max_length=100)
ty_reason = models.CharField('Reason for sending', max_length=200)
fb_anon = models.BooleanField('Anonymous?', default=False)
pub_date = models.DateTimeField('Date Published', auto_now_add=True)
def __unicode__(self):
return self.mod_status
thank_you / urls.py
from django.conf.urls import patterns, url
from thank_you import views
urlpatterns = patterns('',
url(r'^$', 'thank_you.views.index'),
url(r'^(?P<thank_you_id>\d+)/$', 'thank_you.views.detail'),
url(r'^new_ty_form/$', 'thank_you.views.new_ty_form'),
)
thank_you / views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect
from django.core.urlresolvers import reverse
from thank_you.models import ThankYou
from thank_you.forms import NewTYForm
def index(request):
latest_ty_list = ThankYou.objects.order_by('-pub_date')[:5]
context = {'latest_ty_list': latest_ty_list}
return render(request, 'thank_you/index.html', context)
def detail(request, thank_you_id):
ty = get_object_or_404(ThankYou, pk=thank_you_id)
return render(request, 'thank_you/detail.html', {'ty': ty})
def new_ty_form(request):
if request.POST:
form = NewTYForm(request.POST)
form.save(commit=True)
return redirect('thank_you.views.index')
else:
form = NewTYForm()
return render_to_response('thank_you/new_ty_form.html', {'form': form}, context_instance=RequestContext(request))
thank_you / forms.py
from django import forms
from thank_you.models import ThankYou
class NewTYForm(forms.ModelForm):
class Meta:
model = ThankYou
fields = ('your_name', 'fb_rec', 'rec_dep', 'ty_reason', 'fb_anon')
的index.html
<!DOCTYPE html>
<html>
<head>
<title>Thank You!</title>
</head>
<body>
{% if latest_ty_list %}
<ul>
{% for ty in latest_ty_list %}
{% if ty.fb_anon %}
<li><a href=“{% url ‘thank_you.views.detail ty.id %}”>Anonymous</a></li>
{% else %}
<li><a href={{ ty.id }}>{{ ty.your_name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No thank yous are available.</p>
{% endif %}
<a href=new_ty_form/>Add a new thank you</a>
</body>
</html>
detail.html
<h1>{{ ty.id }}</h1>
{% if ty.fb_anon %}
<h1>Anonymous</h1>
{% else %}
<h1>{{ ty.your_name }}</h1>
{% endif %}
<h1>{{ ty.fb_rec }}</h1>
<h1>{{ ty.rec_dep }}</h1>
<h1>{{ ty.ty_reason }}</h1>
<h1>{{ ty.pub_date }}</h1>
new_ty_form.html
<html>
<head>
<title>New Thank You</title>
</head>
<body>
<h1>Thank You</h1>
<form id=“new_ty_form” method=“post” action=“/thank_you/new_ty_form/“>
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<input type=submit value=Submit>
</form>
</body>
</html>
很抱歉代码超载,我只是觉得我错过了一些小东西,我找不到它。我已经浏览了Django文档和教程,但没有运气。