簡體   English   中英

Django-不保存在PostGresql數據庫中

[英]Django - not saving in PostGresql database

請事先原諒我-我知道有一些問題-但我確實沒有找到解決方案。

我在Django上工作,我想填充一個由於創建的模型而創建的Postgresql表。
我還創建了表單,html頁面(模板)和視圖。 我沒有錯誤消息,但是沒有任何內容創建到數據庫中。

這是我的模型

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',         
    }
}

您想使用mysql數據庫,但是在settings.py設置了'ENGINE': 'django.db.backends.postgresql_psycopg2'此設置適用於PostgreSQL,而不適用於MySQL。 您必須將此字符串更改為以下字符串: 'ENGINE': 'django.db.backends.mysql'

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM