gpt4 book ai didi

python - Django save() 方法不向我的 Mysql 数据库插入数据

转载 作者:行者123 更新时间:2023-11-29 05:07:57 25 4
gpt4 key购买 nike

我尝试使用 Django 将数据输入到 Mysql 数据库中。用户通过addSubscriber.html页面输入需要的数据保存到数据库中,但是输入的数据并没有保存到数据库中。每当我检查 Mysql 表(在本例中为“订阅者”表)时,该表都是空的。我是否应该安装 mysql-connecter?

这是我的文件: View .py

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views import generic
from .models import Subscribers
from .forms import addSubsForm



@login_required
def dashboard(request):
user = request.user
context = {'user': user}
template = 'dashboard.html'
return render(request, template, context)



@login_required
def addSubscriber(request):
template = 'addSubscriber.html'
if request.method == 'POST':
form = addSubsForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
area = form.cleaned_data['area']
phoneNumber = form.cleaned_data['phoneNumber']
installationCost = form.cleaned_data['installationCost']
billNumber = form.cleaned_data['billNumber']
print name
Subs = Subscribers.objects.create(name=name, area=area, phoneNumber=phoneNumber, installationCost=installationCost, billNumber=billNumber)
Subs.save()
return redirect('report')

else:
form = addSubsForm()

return render(request, template, {'form': form})




@login_required
def report(request):
context = locals()
template = 'report.html'
return render(request, template, context)

模型.py

from __future__ import unicode_literals
from django.db import models


class Subscribers(models.Model):
name = models.CharField(max_length=120)
area = models.CharField(max_length=30)
phoneNumber = models.CharField(max_length=10)
installationCost = models.CharField(max_length=10)
billNumber = models.CharField(max_length=100, unique=True)

addSubscriber.html

{%extends 'base.html'%}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block content %}



<div class="row">
<div class="col-xs-7">

{% if form %}
<br>


<form class="POST" action="." method="post">
{% csrf_token %}

{{ form|crispy }}
<input type="submit" value="submit form" class="btn btn-primary"/>



</form>

{% endif %}
</div>

</div>
{% endblock %}

网址.py

from dashboard import views as dashboard_views
from profiles import views as profiles_views

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin


urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', profiles_views.home, name='homepage'),
url(r'^contact/$', profiles_views.contact, name='contact'),
url(r'^dashboard/$', dashboard_views.dashboard, name='dashboard'),
url(r'^dashboard/addSubscriber/$', dashboard_views.addSubscriber, name='addSubscriber'),
url(r'^dashboard/userDetail/$', dashboard_views.userDetail, name='userDetail'),
url(r'^dashboard/report/$', dashboard_views.report, name='report'),


url(r'^account/', include('allauth.urls')),

]

# this statement says that: if in the settings file, DEBUG is true than use this static URL
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

最佳答案

由于您使用两个数据库,为了保存实体,您应该通过指定来指定要保存的数据库

Subs.save(using='db2')

根据 Django documentation :

If you don’t specify using, the save() method will save into the default database allocated by the routers.

关于python - Django save() 方法不向我的 Mysql 数据库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com