如何在 django 的 CreateView 中使用上下文与类? [英] How to use context with class in CreateView in django?

查看:20
本文介绍了如何在 django 的 CreateView 中使用上下文与类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 django 中的 CreateView 中使用上下文与类?

在我拥有之前:

#views.py从 django.views.generic 导入 CreateView从cars.models 导入*def CreateCar(CreateView):info_sended = 假如果 request.method == 'POST':form = FormCar(request.POST, request.FILES)如果 form.is_valid():info_sended = 真表单.save()别的:表单 = FormCar()ctx = {'form': form, 'info_sended':info_sended}return render_to_response("create_car.html", ctx,context_instance=RequestContext(request))

现在,尝试一下:

class CreateCar(CreateView):info_sended = 假模板名称 = 'create_car.html'模型 = 汽车success_url = 'create_car' #urls 名称def form_valid(self, form):info_sended = 真ctx = {'form': form, 'info_sended':info_sended}返回 super(CreateCar, self).form_valid(form)

我的 html 页面是:

{% 扩展 'base.html' %}{% 块内容 %}{% if info_sended %}<p>数据保存成功</p><p><a href="{% url 'list_cars' %}">显示列表</a></p>{% 别的 %}<form class="form-horizo​​ntal" action="" method="post">{% csrf_token %}{% 包含 "form.html" %}<div class="col-md-offset-1"><button class="btn btn-primary" type="submit">添加</button>

</表单>{% 万一 %}{% 结束块 %}

解决方案

你应该定义 get_context_data() 方法在类视图中.将您的代码更新为

from django.shortcuts 导入渲染类 CreateCar(CreateView):info_sended = 假模板名称 = 'create_car.html'模型 = 汽车success_url = 'create_car' #urls 名称def form_valid(self, form):self.info_sended = True# 不是返回这个 HttpResponseRedirect,而是返回一个# 新渲染的页面super(CreateCar, self).form_valid(form)返回渲染(self.request,self.template_name,self.get_context_data(form=form))def get_context_data(self, **kwargs):ctx = super(CreateCar, self).get_context_data(**kwargs)ctx['info_sended'] = self.info_sended返回 ctx

How to use context with class in CreateView in django?

Before i have:

#views.py
from django.views.generic import CreateView
from cars.models import *

def CreateCar(CreateView):
    info_sended = False
    if request.method == 'POST':
        form = FormCar(request.POST, request.FILES)
        if form.is_valid():
            info_sended = True
            form.save()
    else:
        form = FormCar()
    ctx = {'form': form, 'info_sended':info_sended}
    return render_to_response("create_car.html", ctx,
        context_instance=RequestContext(request))

Now, a have, and try:

class CreateCar(CreateView):
    info_sended = False
    template_name = 'create_car.html'
    model = Car
    success_url = 'create_car' #urls name

    def form_valid(self, form):
        info_sended = True
        ctx = {'form': form, 'info_sended':info_sended}
        return super(CreateCar, self).form_valid(form)

My html page is:

<!-- create_car.html -->
{% extends 'base.html' %}

{% block content %}

{% if info_sended %}
    <p>Data saved successfully</p>
    <p><a href="{% url 'list_cars' %}">Show List</a></p>
{% else %}
    <form class="form-horizontal" action="" method="post">
        {% csrf_token %}
        {% include "form.html" %}
        <div class="col-md-offset-1">
            <button class="btn btn-primary" type="submit">Add</button>
        </div>
    </form>
{% endif %}

{% endblock %}

解决方案

You should define get_context_data() method in your class view. Update your code as

from django.shortcuts import render

class CreateCar(CreateView):
    info_sended = False
    template_name = 'create_car.html'
    model = Car
    success_url = 'create_car' #urls name

    def form_valid(self, form):
        self.info_sended = True
        # Instead of return this HttpResponseRedirect, return an 
        #  new rendered page
        super(CreateCar, self).form_valid(form)
        return render(self.request, self.template_name,
                      self.get_context_data(form=form))


    def get_context_data(self, **kwargs):
        ctx = super(CreateCar, self).get_context_data(**kwargs)
        ctx['info_sended'] = self.info_sended
        return ctx

这篇关于如何在 django 的 CreateView 中使用上下文与类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆