Django 1.6 trying to create new database entry from form -
i trying develop thank-you note system using django. have set admin ok , able add new entries database through that. have been able set index shows current thank-you notes in database, , detail view shows full details given thank-you note. trying create form view lets people without admin access add thank-you notes system. form loads fine , fields can filled everytime press submit 404 error. way doesn't happen if set form action nothing:
<form id=“new_ty_form” method=“post” action=>
and pressing submit regenerates blank form , nothing previous fields.
i unable implement {% url %} system means have hardcode urls using. whenever try use {% url %} following error:
templatesyntaxerror @ /thank_you/ not parse remainder: '‘thank_you.views.detail' '‘thank_you.views.detail'. syntax of 'url' changed in django 1.5, see docs.
i stuck on this, please can help? include of code can.
mysite/settings.py
""" django settings mysite project. more information on file, see https://docs.djangoproject.com/en/1.6/topics/settings/ full list of settings , values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ # build paths inside project this: os.path.join(base_dir, ...) import os base_dir = os.path.dirname(os.path.dirname(__file__)) # quick-start development settings - unsuitable production # see https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # security warning: keep 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 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 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 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 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 django.shortcuts import render_to_response django.http import httpresponseredirect django.shortcuts import render, get_object_or_404, redirect django.core.urlresolvers import reverse thank_you.models import thankyou 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 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> {% 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 available.</p> {% endif %} <a href=new_ty_form/>add 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>
sorry code overload, have feeling i'm missing small , can't find it. i've scoured django docs , tutorials no luck.
Comments
Post a Comment