简体   繁体   中英

How can I get data from a form model into a database in Django

I am trying to get data from a model form and then put it into a database. I have figured out how to make the form, but when clicking the submit button it doesn't seem to be put anywhere in my database. Am I doing anything wrong or am I not looking in the correct place in the database.

forms.py

from django import forms
from sxsw.models import Account

class AccountForm(forms.ModelForm):
    class Meta:
        model = Account
        fields = ['firstName', 'lastName', 'email']

views.py

from django.shortcuts import render
from django.shortcuts import redirect
from .forms import AccountForm
from .models import Account 

def sxsw(request):
    if request.method == 'POST':
        form = AccountForm(request.POST)
        if form.is_valid():
            form.save()
        else:
            print form.errors
    else:
        form = AccountForm()

    return render(request, 'sxsw/sxsw.html', {'form': form})

def formSubmitted(request):
    return render(request, 'sxsw/formSubmitted.html',{})

models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Account(models.Model):
    firstName = models.CharField(max_length = 50)
    lastName = models.CharField(max_length = 50)
    email = models.EmailField()

    def __unicode__(self):
        return self.firstName

class Module(models.Model):
    author = models.ForeignKey(Account, on_delete = models.CASCADE)
    nameOfModule = models.CharField(max_length = 150) #arbitrary number
    moduleFile = models.FileField(upload_to = 'uploads/')#Not exactly sure about the upload_to thing
    public = models.BooleanField()

    def __unicode__(self):
        return self.nameOfModule

sxsw.html

{% extends "base.html" %}

{% block content %}
    <div class="container-fluid">
        <div class="jumbotron text-center">
          <h3>SXSW Form</h3> 
        </div>

    </div>

    <div align="center">
        <h1>New Form</h1>
        <form role='form' action="/sxsw/formSubmitted/" method="post">
            {% csrf_token %}
            {{ form.as_p }}
            <button type="submit">Submit</button>
        </form>
    </div>


  </div>
{% endblock %}

formSubmitted.html

{% extends "base.html" %}

{% block content %}
    <div class="container-fluid">
        <div class="jumbotron text-center">
          <h3>Form Successfully submitted</h3> 
        </div>

    </div>

    <div align="center">
        <a href="{% url 'sxsw' %}" class="btn">Submit Another Response</a>
    </div>


  </div>
{% endblock %}

Your form is posting to what I presume is the wrong url

 <form role='form' action="/sxsw/formSubmitted/" method="post">

should use the url for the sxsw view

 <form role='form' action="/sxsw/" method="post">

Once submitted, you'll likely want to redirect to the submitted view

 return redirect('/sxsw/formSubmitted/')  # Preferably use the url pattern name here

看起来您的表单的action设置为/sxsw/formSubmitted/ ,该操作总是简单地返回提交的页面,而不是将调用sxsw视图的URL,在该sxsw视图中调用了表单的save方法。

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