Django投票表格

时间:2018-09-27 20:42:15

标签: python django

我有一个包含两个字段的Web表单:

  • gender:男性或女性
  • diseases:HIV,疟疾,伤寒

我创建了一个投票提交按钮。我希望该投票按钮指向另一种记录用户选择的表格(genderdiseases),例如,患有艾滋病毒,霍乱(疾病字段)等的男性和女性人数。

我该怎么办?

输出示例:

Male-cholera-2
Male-Thypoid-5
Female-HIV-4
Female-Cholera-3

Views.py

from django.views.generic import ListView, CreateView, UpdateView
from django.urls import reverse_lazy
from . models import MyModel
from . forms import MyModelForm

class CreateMyModelView(CreateView):
    model = MyModel
    form_class = MyModelForm
    template_name = 'poll/template.html'

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.CreateMyModelView.as_view(), name='abc'),
]

models.py

from django.db import models

Gender = (
    ('male', 'MALE'),
    ('female', 'FEMALE'),
)
Diseases = (
    ('cholera', 'CHOLERA'),
    ('hiv', 'HIV'),
    ('Malaria', 'MALARIA'),
    ('Typhoid', 'TYPHOID'),
)

class MyModel(models.Model):
    Gender = models.CharField(max_length=16, choices=Gender, default='MALE')
    Diseases = models.CharField(max_length=16, choices=Diseases, default='MALARIA')

forms.py

from .models import MyModel
from django import forms

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['Gender', 'Diseases']

template.html

{% extends 'poll/base.html' %}
{% block main_content %}
<h1></h1>
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Vote" />
</form>

{% endblock %}

0 个答案:

没有答案