2017-06-30 111 views
0

我試圖使用Django將數據輸入到Mysql數據庫。用戶通過addSubscriber.html頁面輸入所需數據以保存在數據庫中,但輸入的數據不保存在數據庫中。每當我檢查Mysql表(在這種情況下是'Subscribers'表)時,表是空的。我應該安裝mysql-connecter嗎?Django save()方法不會將數據插入到我的Mysql數據庫中

這裏是我的文件: views.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) 

models.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 %} 

urls.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) 
+0

你可以po這裏的'DATABASES'設置? – Arun

+0

由於objects.create保存了對象本身,因此對Subs.save()的調用可能已過時。 https://docs.djangoproject.com/en/1.11/ref/models/querysets/#create – tback

回答

1

那麼你不需要在視圖中所有這些額外的東西,因爲你正在使用的形式,爲什麼使用createsave

只是嘗試這個form.is_valid

 if form.is_valid(): 
      f = form.save(commit=False) 
      f.save(using='db2') 
      return redirect('report') 
+0

如果我不使用'form.cleaned_data'將不會保存我輸入的加密形式的數據? –

+0

是的,它會,只需嘗試一次,讓我知道你看到了什麼 – Exprator

+0

顯示相同的錯誤。我嘗試了@Arun給出的解決方案,並且發佈了應用它後得到的錯誤。請看看它。 –

2

內由於使用的兩個數據庫,用於保存實體應指定的數據庫來保存指定

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.

+0

我認爲它的工作,但仍然數據庫有一些問題。 'django.db.utils.IntegrityError:UNIQUE約束失敗:當我嘗試'python manage.py migrate'時顯示dashboard_subscribers.billNumber'# –

+0

@ManishOjha:嘗試https:// stackoverflow。com/a/34650891/6619424 – Arun

+0

我剛剛刪除了unique = True和應用makemigrations並遷移,但是每當我使用python manage.py遷移時,錯誤都是一樣的 –

相關問題