Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未使用Django在sqlite数据库中保存数据_Python_Django_Web - Fatal编程技术网

Python 未使用Django在sqlite数据库中保存数据

Python 未使用Django在sqlite数据库中保存数据,python,django,web,Python,Django,Web,我在使用Django将数据保存到sqlite数据库时遇到了问题。 当我访问127.0.0.1/personal/makeentry-->时,页面显示表单,但当我在输入详细信息后单击submit时,详细信息不会保存在db中 下面是代码 model.py from django.db import models class Account(models.Model): accountId = models.IntegerField(primary_key=True,unique=True

我在使用Django将数据保存到sqlite数据库时遇到了问题。 当我访问127.0.0.1/personal/makeentry-->时,页面显示表单,但当我在输入详细信息后单击submit时,详细信息不会保存在db中

下面是代码 model.py

from django.db import models


class Account(models.Model):
    accountId = models.IntegerField(primary_key=True,unique=True, 
    blank=False, null=False, auto_created=True)
    accountName = models.CharField(max_length=100)
    countryName = models.CharField(max_length=100)
    marketName = models.CharField(max_length=100)
    msdmName = models.CharField(max_length=100)
    deliveryManagedFrom = models.CharField(max_length=100)
    location = models.CharField(max_length=100)

    def __str__(self):
        return self.accountName
管理员

from django.contrib import admin
from .models import Account

# Register your models here.

class AccountAdmin(admin.ModelAdmin):
    list_display = ['accountId', 'accountName','countryName', 'marketName', 
    'msdmName','deliveryManagedFrom','location']


admin.site.register(Account, AccountAdmin)
forms.py

from django import forms
from . models import Account
from django.forms import ModelForm

class AccountForm(forms.Form):
    accountName = forms.CharField(label="Account Name", max_length=100)
    countryName = forms.CharField(label="Country Name", max_length=100)
    marketName = forms.CharField(label="Market Name", max_length=100)
    msdmName = forms.CharField(label="MSDM Name", max_length=100)
    deliveryManagedFrom = forms.CharField(label="Delivery Managed From", max_length=100)
    location = forms.CharField(label="Location", max_length=100)

class AccountForm(ModelForm):
    class Meta:
        model = Account
        fields = ['accountName','countryName', 'marketName', 'msdmName', 'deliveryManagedFrom', 'location']
views.py

from django.shortcuts import render
from django.views import generic
from .models import Account
from .forms import AccountForm


def index(request):
    return render(request, 'personal/home.html')


# generic view to fetch the data then show in a list
class IndexView(generic.ListView):
    # a name to refer to the object_list(to be used in the index.html)
    context_object_name = 'account_list'
    template_name = 'personal/index.html'

    def get_queryset(self):
        return Account.objects.all()


# generic view to show the details of a particular object
class DetailsView(generic.DetailView):
    model = Account
    template_name = 'personal/detail.html'

def makeentry(request):
    if request.method == 'POST':
        form = AccountForm(request.POST)

        if form.is_valid():
            accountName = request.POST.get('Account Name', '')
            countryName = request.POST.get('Country Name', '')
            marketName = request.POST.get('Market Name', '')
            msdmName = request.POST.get('MSDM Name', '')
            deliveryManagedFrom = request.POST.get('Delivery Managed From', '')
            location = request.POST.get('Location', '')

        account = Account(accountName=accountName, countryName=countryName, marketName=marketName, msdmName=msdmName, deliveryManagedFrom=deliveryManagedFrom, location=location)
        account.save()

        form = AccountForm()
        return render(request, 'personal/makeentry.html', {'form': form})


    else:
        form = AccountForm()
        return render(request, 'personal/makeentry.html', {'form': form})
makeentry.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Account Entry</title>
</head>
<body>
    <form action="{% url 'makeentry' %}" method="post">

    {% csrf_token %}
   {{ form }}
    <input type="submit" value="Submit">
    </form>


</body>
</html>

帐户分录
{%csrf_令牌%}
{{form}}
detail.html

{% if account_list %}
   <table>
      <tr>
         <td> Account Name </td>
         <td> Country Name </td>
         <td> Market Name </td>
         <td> MSDM Name </td>
         <td> Delivery Managed From </td>
         <td> Location </td>
      </tr>
      {% for account in account_list %}
      <tr>
         <td>{{ account.accountName }}</td>
         <td>{{ account.countryName }}</td>
         <td>{{ account.marketName }}</td>
         <td>{{ account.msdmName }}</td>
         <td>{{ account.deliveryManagedFrom }}</td>
         <td>{{ account.location }}</td>
      </tr>
      {% endfor %}
   </table>
{% endif %}
{%if账户\列表%}
帐户名
国名
市场名称
MSDM名称
从管理的交付
位置
{帐户列表%中的帐户的%0}
{{account.accountName}
{{account.countryName}
{{account.marketName}
{{account.msdname}
{{account.deliveryManagedFrom}
{{account.location}
{%endfor%}
{%endif%}

我认为你让这件事变得更加困难了。您有一个同名的
表单
和一个
模型表单
。您应该能够使用
模型表单
。如果您想通过
ModelForm
传递属性,请检查覆盖默认小部件。我的经验还包括设置
action=”“
,并让视图在成功时处理重定向

def make_entry(request):

if request.POST:
    form = AccountForm(request.POST)

    if form.is_valid():
        new_form = form.save()
        return redirect('where-you-want-to-redirect ',
            # View current saved info or change to what you want to show
                          pk=new_form.pk)

该表单可能无效,但您可以通过始终创建新表单实例来隐藏任何错误。不要那样做。如果表单有效,则保存并重定向;如果无效,则重新显示现有表单\