Django发布数据未保存在管理数据库中

时间:2019-04-05 13:05:18

标签: python html django

我希望将表单数据保存在数据库中,但是它不起作用 另外,我如何将帖子数据保存到计算机中?(如果这个问题不清楚,您可以忽略)

view.py

class task_generation_view(FormView):
    form_class = TaskGenerationForm
    model = TaskGeneration
    fields = '__all__'
    template_name = "task_generation.html"
    success_url = reverse_lazy('data_upload')

models.py

class TaskGeneration(models.Model):
    classification = '분류 알고리즘'
    regression = '회귀 알고리즘'
    clustering ='군집화 알고리즘'
    detection = '이상치 탐지 알고리즘'
    reinforce ='강화학습 알고리즘'
    SELECT_ALGORITHM_CHOICES = (
        (classification, '분류 알고리즘'),
        (regression,'회귀 알고리즘'),
        (clustering, '군집화 알고리즘'),
        (detection, ' 이상치 탐지 알고리즘'),
        (clustering,' 강화학습 알고리즘'),
    )
    algorithm = models.CharField(max_length=30, choices = SELECT_ALGORITHM_CHOICES, default = classification)
    readyData = models.BooleanField()

urls.py

urlpatterns = [
    #url('$', ModelingView_model.as_view()),
    url('task_generation', task_generation_view.as_view()),
    url('data_upload', data_upload_view.as_view(), name='data_upload'),
    url('data_load', data_load_view.as_view()),
    url('data_exploration', data_exploration_view.as_view(), name='data_exploration'),
    url('data_variable_Identification', data_variable_identification_view.as_view()),

    # url('', FileUploadView.as_view(), name='upload'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

forms.py

class TaskGenerationForm(forms.ModelForm):
    class Meta:
        model = TaskGeneration
        fields = ('algorithm', 'readyData')
        widgets ={
            'algorithm' : forms.Select(attrs = {'class' : 'btn btn-info btn-select btn-select-light'}),
        }

task_generation.html

 <form method = "post" enctype="multipart/form-data">
...
  {% csrf_token %}
  {{ form.algorithm }}
...
  {% csrf_token %}
  {{ form.readyData }}
<button type="submt">submit</button>
 </form>

1 个答案:

答案 0 :(得分:0)

为将来参考,task_generation_view中基于类的视图views.py继承自错误的类。它应该扩展CreateView而不是FormView。我建议在Django文档网站here上阅读有关CBV的更多信息。