【问题标题】:How create a custom form in django with database values如何在 django 中使用数据库值创建自定义表单
【发布时间】:2014-03-25 00:16:08
【问题描述】:

模型.py

class Venue(models.Model):
    venue_Name = models.CharField(max_length=100)
    place = models.CharField(max_length=50)
    rent = models.IntegerField()
    parking_area = models.IntegerField()


class Decoration(models.Model):
    rate = models.IntegerField()

我已将数据库中的值打印为单选按钮我想要做的是我想要获得总和,即场地租金 + 装饰费率并将其打印在另一个页面中我应该在表单操作中给出什么?我对表格不太熟悉。

html

 <form action="{%  %}" method="post">
 {% for venue in venues %}
 <input type="radio" name=venue value=venue.rent />{{  venue.venue_Name}}
 {% endfor %}

{% for decoration in decorations %}
<input type="radio" name=decor value=decoration.rate />{{  decoration.rating }}
{% endfor %}
<input type="submit" value=" " />
</form>

我应该在视图和网址中写什么来得到总和

【问题讨论】:

    标签: python django django-templates django-views django-urls


    【解决方案1】:

    您可以使用 Django 的表单进行验证和解析。为此,您可以像这样设置一个表单:

    forms.py

    from django import forms
    
    class TotalSumForm(forms.Form):
        venue = forms.ModelChoiceField(queryset=Venue.objects.all(), required=True)
        decoration = forms.ModelChoiceField(
            queryset=Decoration.objects.all(), required=True)
    
        def get_total(self):
            # send email using the self.cleaned_data dictionary
            return self.cleaned_data['venue'].rent +\
                   self.cleaned_data['decoration'].rate
    

    然后使用基于类的视图,在提交时将结果添加到上下文中。

    views.py

    from myapp.forms import TotalSumForm(
    from django.views.generic.edit import FormView
    
    class TotalCost(FormView):
        template_name = 'your_template.html'
        form_class = TotalSumForm
        success_url = '/thanks/'
    
        def form_valid(self, form):
            # This method is called when valid form data has been POSTed.
            total_result = form.get_total()
            # return back to your_template.html with the total in context
            return self.render_to_response(self.get_context_data(
                form=form, total=total_result))
    

    网址非常简单:

    urls.py

    from django.conf.urls import patterns, url
    
    import myapp.views
    
    urlpatterns = patterns(
        '',
        url(r'^total_calc/$', myapp.views.TotalCost.as_view(), name='calculate_total'),
    )
    

    你的 html 可以这样修改

    your_template.html

    <html>
        <body>
            <h1>TEST SUCCESFULL!</h1>
                {% if total %}
            <p>Total cost for venue and decoration: {{ total }}</p>
        {% endif %}
            <form action="{% url 'calculate_total' %}" method="post">
                {{ form.as_p }}
                <input type="submit" value="Calculate Total" />
            </form>
        </body>
    </html>
    

    【讨论】:

    • 您的 ModelChoiceField 行有错字吗?我认为查询集丢失了。
    • 出现错误 AttributeError 异常值:'module' 对象没有属性 'ModelChoiceField'
    • 啊,打错了,应该是forms.ModelChoiceField 而不是models.ModelChoiceField。更新了答案
    • 如果我在我的模型中添加图像字段,请说图片=model.ImageField (upload_to="image/");我如何以表格形式打印它
    • 我假设您只是想在模板中为每个场地/装饰描述/标签简单地显示一个图像?这会让你有点脱离标准形式......你为什么不在一个单独的问题中问这个?
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多