简体   繁体   中英

model forms were not generated automatically using django model forms

I'm creating Django forms using model forms because u I wanted the forms to be created automatically, but when I created this code the forms do not appear in the index.html page

models.py

from django.db import models


class BaseCase(models.Model):
    base_case_name = models.CharField(primary_key=True, max_length=255)
    version = models.TextField(blank=True, null=True)
    default = models.TextField(blank=True, null=True)  # This field type is a guess.

    class Meta:
        managed = False
        db_table = 'base_case'

forms.py

from django import forms
from SFP.models import *


class BaseCaseForm(forms.ModelForm):
    class Meta :
        model = BaseCase
        fields='__all__'

views.py

from django.shortcuts import render,redirect
from .models import *
from .forms import *

def addbc(self, request):
    bcform=BaseCaseForm(request.POST)
    bcform.save()
    basecasename = bcform.cleaned_data['post']
    version = bcform.cleaned_data['post']
    default = bcform.cleaned_data['post']
    bcform = BaseCaseForm()
    return redirect('index.html')
    args = {'bcform':bcform,
            'basecasename': basecasename,
            'version': version,
            'default' :default}

    return render(request, 'index.html', args)

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>S&FP</title>
    </head>
    <body>
        <h1>Forms</h1>
          {% csrf_token %}
          {{ bcform }}
       <input type="submit" value="add">
    </body>
</html>

and i think that this is important too urls.py

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

urlpatterns = [
    url(r'^$', views.index),
    url(r'^$', views.addbc),
]

I was expecting the form fields to be generated automatically but they don't appear!

Your whole view is wrong.Change like this

def addbc(request):
    bcform = BaseCaseForm()
    if request.method == 'POST'
       bcform=BaseCaseForm(request.POST)
       if bcform.is_valid():
         bcform.save()
         return redirect('some_path') #redirect to some url not template

    return render(request, 'index.html', {'bcform':bcform})

EDIT: There is no <form> tag in your html.Add this

<h1>Forms</h1>

<form method='post'>
          {% csrf_token %}
          {{ bcform }}
       <input type="submit" value="add">
</form>

Your views.index and views.addbc using the same empty path change like this

 url(r'^addbc/^$', views.addbc),

Also use path if your using django 2+

You can try CreateView which will create forms for your model. Find more about it in the docs

In your case, create a view like this:

views.py

class BaseCaseCreate(CreateView):
    model = BaseCase
    template_name = 'index.html'
    success_url = reverse_lazy('app:home')
    fields = ('base_case_name','version','default')

index.html

<!DOCTYPE html>
<html>
<head>
    <title>S&FP</title>
</head>
<body>
    <h1>Forms</h1>
      {% csrf_token %}
      {{ form }}
   <input type="submit" value="add">
</body>

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM