无法将字段中的值添加到数据库Django中

时间:2017-06-30 16:12:20

标签: python django database

我想将数据填充到数据库中并输出。但我不知道哪里出错了,因为我的数据根本没有保存到数据库中。在views.py中,奖学金只是一个奖学金对象,LS奖学金显示奖学金中的所有数据。我有其他模型和视图的类似代码,但我不知道我在这里做错了什么,使得数据无法保存到数据库中。任何人都可以建议我在哪里错了

add_remove_scholarship.html

 <div align="center" >
    <form method="POST" onsubmit="return validation()"  action="">
    {% csrf_token %}
    {{ form.errors }}
    <p>Upload File: {{scholarship.doc}} <input id="doc" type="text" name="doc"> </p>
    <p>Faculty: {{scholarship.faculty}} <input id="faculty" type="text" name="faculty"> </p>
    <p>Opening date: {{scholarship.openDate}} <input id="odate" type="date" name="openDate"> </p>
    <p>Closing date: {{scholarship.closeDate}} <input id="edate" type="text" name="closeDate"> </p>
    <input type="submit" name="AddScholarship" value="Add Scholarship"   >
    </form>
    </div>
    <br></br>
    <button id="button" type="button">Delete Selected Scholarship</button>
    <br></br>
<form method="POST" action="">
    {% csrf_token %}
    <table  id="example" class="display" cellspacing="0" width="100%" border="1.5px">
        <tr align="center">
            <th> Scholarship </th>
            <th> Faculty </th>
            <th> Open Date </th>
            <th> Close Date </th>
        </tr>
        {% for item in query_results %}
        <tr align="center">
            <td>{{item.doc}}</td>
            <td>{{item.faculty}}</td>
            <td>{{item.openDate}}</td>
            <td>{{item.closeDate}}</td>
        </tr>
        {% endfor %}
        </table>
</form> 

models.py

#consists of all the details of the scholarship under the 'Add/Remove 
Scholarship'
class Scholarship(models.Model):
    doc = models.TextField("Doc", max_length=1000)
    faculty = models.TextField("Faculty", max_length=1000)
    openDate = models.DateField("Opening Date", max_length=8)
    closeDate = models.TextField("CLosing Date", max_length=18)

    def __str__(self):
        return self.doc

#consists of all the details of the scholarship under the 'Add/Remove 
Scholarship'
class LScholarship(models.Model):
    scholarship = models.ForeignKey(Scholarship, on_delete=models.CASCADE)

views.py

def scholarship(request):
    if request.method == "POST":
        form = ScholarshipForm(request.POST)
        if form.is_valid():
            scholarship = form.save(commit=False)
            scholarship.save()
    else:
        form = ScholarshipForm()
    return render(request, 'hrfinance/add_remove_scholarship.html', {'form': form})

def lscholarship(request):
    query_results = Scholarship.objects.all()
    data={'query_results':query_results}
    return render(request, 'hrfinance/add_remove_scholarship.html', data)

class ScholarshipForm(forms.ModelForm):
    class Meta:
        model = Scholarship
        fields = '__all__'

1 个答案:

答案 0 :(得分:0)

您正在使用两个视图来呈现单个模板。

您可以将视图合并为一个视图,也可以减少逻辑。 如果我已正确理解您的问题,您可以使用更多这样的视图,

def scholarship(request, id=None):
    query_results = []
    if request.method == "POST":
        form = ScholarshipForm(request.POST)
        if form.is_valid():
            scholarship = form.save(commit=False)
            scholarship.save()
    else:
        form = ScholarshipForm()
        id = request.GET.get('scholarship')
        query_results = Scholarship.objects.all()
    data = {
            'query_results':query_results,
            'form': form,
           }
    return render(request, 'hrfinance/add_remove_scholarship.html', data)

在您的模板中

<div align="center" >
    <form method="POST" onsubmit="return validation()" action="">
    {% csrf_token %}
    {{ form.errors }}
        <p>Upload File: {{ form.doc }}</p>
        <p>Faculty: {{ form.faculty }} </p>
        <p>Opening date: {{ form.startDate }} </p>
        <p>Closing date: {{ closeDate }} </p>
        <button type="submit" name="AddUser" value="Add Scholarship" onclick="add()"  >Add Scholarship</button>
    </form>
</div>
    <table  id="example" class="display" cellspacing="0" width="100%" border="1.5px">
        <tr align="center">
            <th> Scholarship </th>
            <th> Faculty </th>
            <th> Open Date </th>
            <th> Close Date </th>
        </tr>
        {% for item in query_results %}
        <tr align="center">
            <td>{{item.doc}}</td>
            <td>{{item.faculty}}</td>
            <td>{{item.openDate}}</td>
            <td>{{item.closeDate}}</td>
        </tr>
        {% endfor %}
        </table>