2017-07-13 35 views
0

請原諒我事先 - 我知道有一些問題上 - 但我真的沒有找到解決方案。Django的 - 在PostgreSQL數據庫不節能

我的Django的工作,我想填充所創建感謝我做了一個模型,一個PostgreSQL表。
我還創建了我的表單,我的html頁面(模板)和我的視圖。我沒有錯誤消息,但沒有任何內容被創建到數據庫中。

這裏是我的model.py

class ModForm1(models.Model) : 
    utilisateur = models.CharField(max_length=100) 
    description = models.CharField(max_length=500) 
    date = models.DateTimeField(auto_now_add=True, auto_now=False, 
           verbose_name="Date de parution") 

    def __unicode__(self): 
     return "{0} {1} {2} {3} ".format(self, self.utilisateur, self.description, self.date) 

這裏是我的form.py

class ModForm1Form(ModelForm): 
    class Meta: 
    model = ModForm1 
    fields = '__all__' 

這裏是我的模板:

<div class="container"> 
    <div style="width:30%"> 
     <form role="form" action="." method="post"> 
      <div class="col-xs-12"> 
       <legend><i class="icon-group"></i>&nbsp;Authentification</legend> 

       {% csrf_token %} 
       <div class="form-group"> 
        <label for="example-text-input">Utilisateur</label> {{ form.utilisateur }} 
       </div> 
       <div class="form-group"> 
        <label for="example-text-input">Description</label> {{ form.description }} 
       </div> 

       <input class="btn btn-primary" type="submit" value="submit" /> 
     </form> 

     </div> 
</div> 

,這裏是我的views.py

def addentry(request): 
    form = ModForm1Form(request.POST) 
    # if this is a POST request we need to process the form data 
    if request.method == 'POST': 
     if form.is_valid(): 
      # process the data in form.cleaned_data as required 
      # ... 
      # redirect to a new URL: 
      form.save() 
      return HttpResponseRedirect('.') 
      #messages.error(request, "Error") 

    # if a GET (or any other method) we'll create a blank form 
     else: 
      messages.error(request, "Error") 

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

我幾乎讀了所有與StackOverflow相關的問題,但是我很困難。我真的不知道爲什麼沒有寫入我的數據庫。
請問您需要注意並幫助我嗎?

謝謝!
朱利安

編輯 - 在setting.py

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', 
     'NAME': 'django', 
     # The following settings are not used with sqlite3: 
     'USER': 'julien', 
     'PASSWORD': '***', 
     'HOST': 'localhost',      # Empty for localhost through domain sockets or   '127.0.0.1' for localhost through TCP. 
     'PORT': '5432',   
    } 
} 
+0

你可以添加數據庫配置(settings.py)嗎?和錯誤消息?不幸的是 –

+0

任何錯誤消息。我貼我的第一篇 – Julien

+1

數據庫配置[包括下列形式的錯誤](https://docs.djangoproject.com/en/1.11/topics/forms/#rendering-fields-manually)在模板,以便您可以看到問題所在。你也可以通過在視圖中檢查'form.errors'進行調試。 – Alasdair

回答

0

你想使用MySQL數據庫的數據庫配置,但在settings.py設置'ENGINE': 'django.db.backends.postgresql_psycopg2'這是設置對PostgreSQL不爲MySQL。您必須將此字符串更改爲以下內容: 'ENGINE': 'django.db.backends.mysql'

希望這是幫助。

+0

我的錯誤。我很抱歉,我的Django的工作在PostgreSQL(編輯製作)而不是Mysql ..這是因爲我在同一時間在另一個項目的Mysql數據庫上工作。 – Julien