如何在 django admin 密码中使用 formset

博客分类:
介绍:mako模版跟据多方测试,是目前渲染最快的模版。好不好用,仁者见仁。
下面是我从网上总结下来的精简版。jinja2 也可以用,只要小改一下。
#mymako.pyfrom django.template.context import Context
from django.http import HttpResponse
from mako.template import Template
from mako.lookup import TemplateLookup
def render_to_response(t,c=None,context_instance=None):
path = os.path.join(os.path.dirname(__file__), 'templates/')
mylookup = TemplateLookup(directories=[path],output_encoding='utf-8',input_encoding='utf-8')
mako_temp = mylookup.get_template(t)
if context_instance:
context_instance.update(c)
context_instance = Context(c)
for d in context_instance:data.update(d)
return HttpResponse(mako_temp.render(**data))
把上面这个 mymako.py 放到 project下,随时就可以调用了,下面是调的例子:
#views.py
from django.template import RequestContext
from mymako import render_to_response
from django import forms
def index(request):
if request.method == 'GET':
form = MyForm()
form = MyForm(request.POST)
return render_to_response('mako_temp.html',{'form':form},RequestContext(request))
class MyForm(forms.Form):
name = forms.CharField(label='name',required=True)
mako_temp.html
&html&
&form action="." method="post"&
${form}&br /&
&input type="submit" value="post"/&
论坛回复 /
(22 / 11649)
Django不爽的地方之一: 没有继承ajax, form也不能用javascript来验证。。。
hehe 看来是被RAILS惯坏了。
aninfeel 写道django看上去很美,遇到特殊定制的很让人抓狂,转到pylons和turbogear了
支持pylons!
我目前也正用 pylons 做一个项目 ,边学边做吧。
hehe
django看上去很美,遇到特殊定制的很让人抓狂,转到pylons和turbogear了
支持pylons!
没看懂,这和form有什么关系
这种需求下你怎么设计django的Form?
对于简单的场景Form足够方便,可是对于复杂的html页面如何应用form才是最佳呢?比如常见的master/detail结构的那种页面,页面的上半部分显示作者的相关信息,下半部分显示这个作者书籍的一个列表,这种情况下django好象没有什么好的办法,我都是从request里直接取值的,很繁。
没看懂,这和form有什么关系
不是有formset吗?
Formset中的Form只能是一种类型
范三山 写道django 除了模板以外 没什么称得上“不好”的地方
form 也不好用
hehe, 我到是感觉 django 的 form 很好用,面面俱到。
django 除了模板以外 没什么称得上“不好”的地方
form 也不好用
& 上一页 1
浏览: 28514 次
来自: 北京
flask会有很好的前景的,我的项目已经全部转向用flask和 ...
最近有更新吗?
why?我在win7下用py2.5装没有问题啊
正在学习当中,支持以下
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'在上一节的教程中,我们介绍了,并编写了一个简单的实例。本小节我们将学习网络投票应用程序,并将侧重于简单的表单处理,以最少代码代码量来实现。
编写一个简单的表单
让我们更新&poll&detail 模板(“polls/detail.html”)&,从上个教程,在模板&polls/templates/polls/detail.html&包含一个HTML&form&元素:
&h1&{{ question.question_text }}&/h1&
{% if error_message %}&p&&strong&{{ error_message }}&/strong&&/p&{% endif %}
&form action="{% url 'polls:vote' question.id %}" method="post"&
{% csrf_token %}
{% for choice in question.choice_set.all %}
&input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /&
&label for="choice{{ forloop.counter }}"&{{ choice.choice_text }}&/label&&br /&
{% endfor %}
&input type="submit" value="Vote" /&
简要介绍:
上面的模板显示每个问题选择一个单选按钮。每个单选按钮的值相联问题的选择编号。每个单选按钮的名称是“choice”。这意味着,当有人选择了其中一个单选按钮并提交表单,它会发送POST数据choice=#,其中#是被选择的选择的ID。这是HTML表单的基本概念。
我们设置表单的动作&{%&url&'polls:vote'&question.id&%}, 以及设置 method="post". 使用 method="post"&(相对于&method="get") 是非常重要的,因为提交此表将改变服务器端数据的行为。当创建一个改变数据服务器端表单形式,使用&method="post". 这篇文章并不是只针对 D 这是一个很好的 Web 开发实践。
forloop.counter表示表单标签通过多少次循环了
因为我们正在创建一个POST形式(可以有修改数据的影响),我们需要担心跨站点请求伪造。但是也不必担心,因为Django自带了保护对抗的一个非常容易使用的系统。总之,这是针对内部URL所有的POST形式应该使用{%csrf_token%}模板标签。
现在,让我们创建一个处理提交的数据的一个 Django 视图。
polls/urls.py文件内容如下:
url(r'^(?P&question_id&[0-9]+)/vote/$', views.vote, name='vote'),
我们还创建了一个虚拟实现 vote() 函数。现在创建一个实用的版本。添加到以下代码到文件 polls/views.py:
polls/views.py 文件的内容如下:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from .models import Choice, Question
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
此代码包含还没有在本教程中涉及几个东西:
request.POST是一个类似于字典的对象,使您可以通过键名访问提交的数据。在这种情况下,request.POST['choice'] 返回被选择的choice的ID,作为字符串。 request.POST的值总是字符串。&
注意:Django还提供 request.GET 以相同的方式访问 GET数据& – 但我们明确使用 request.POST 在我们的代码,以确保数据只能通过POST调用修改。
如果POST数据未提供choice,request.POST['choice']将引发KeyError异常。上面的代码检查KeyError异常和错误消息显示问题的表单,如果没有给出&choice。
选择choice计数递增后,代码返回 HttpResponse 重定向,而不是一个正常的 HttpResponse。HttpResponseRedirect 需要一个参数:用户将被重定向到URL(请参阅下面-我们如何构建在这种情况下的URL)。
如上Python的注释所指出的,应该总是在 POST 数据处理成功后返回一个HttpResponse重定向。
在本例中我们使用的是 HttpResponseRedirect 构造reverse()函数。此函数有助于避免硬编码URL在视图中。这是因为我们想通过控制并指向该视图的URL模式的可变部分的视图的名称。在这种情况下,使用 URLconf 配置使 reverse()调用返回字符串如:
'/polls/3/results/'
其中3是question.id的值。然后,这个重定向的URL将调用“results”视图中显示的最后一页。
现在访问网址:http://127.0.0.1:8000/polls/1/ 得到结果如下所示:
当有人在一个问题投票后,vote() 视图重定向到该问题的结果页面。让我们编写这个视图(polls/views.py):
from django.shortcuts import get_object_or_404, render
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
现在,创建一个 polls/results.html (polls/templates/polls/results.html)模板:
&h2&{{ question.question_text }}&/h2&
{% for choice in question.choice_set.all %}
&li&{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}&/li&
{% endfor %}
&a href="{% url 'polls:detail' question.id %}"&Vote again?&/a&
现在,在浏览器中打开 /polls/1/ 并表决的问题。应该会被每次投票时看到更新结果页。如果您提交表单不选择一个选项,应该看到错误消息。
选择选项,提交后显示如下结果:
使用通用视图:更少的代码更好
修改URL配置
首先,打开 polls/urls.py 并修改如下:
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P&pk&[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P&pk&[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P&question_id&[0-9]+)/vote/$', views.vote, name='vote'),
请注意,第二和第三模式的正则表达式匹配的模式名称已经从&question_id&改变为&to&。
接下来,我们要删除旧的 index, detail, 和 results 视图使用Django通用视图代替。要做到这一点,打开 polls/views.py 文件并修改它如下:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
... # same as above
剩下的你自己发挥了,包教不包会,请参考:
代码下载:
加QQ群啦!
JAVA技术QQ群:
MySQL/SQL语句QQ群:
Python QQ群:
大数据开发技术:
易百教程移动端:请扫描本页面底部(右侧)二维码关注微信公众号,或直接手机访问:
上一篇:下一篇:哥,这回真没有了拒绝访问 |
| 百度云加速
请打开cookies.
此网站 () 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(3ada8-ua98).
重新安装浏览器,或使用别的浏览器

我要回帖

更多关于 django admin 美化 的文章

 

随机推荐