简体   繁体   中英

Django - not saving in PostGresql database

Please forgive me in advance - I know there are some questions on that - but I really didn't find the solution.

I work on Django and I want to populate a Postgresql table that was created thanks to a model I made.
I also created my form, my html page (template) and my view. I have no error message but nothing is created into the database.

Here is my 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)  

Here is my form.py

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

Here is my template :

<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>

And here is my 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})

I read nearly all the questions related in StackOverflow but I'm really stuck. I don't really find why nothing in written into my database.
Could you take an eye and help me please ?

Thanks !
Julien

EDIT - The database config in 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',         
    }
}

You want to use mysql database, but in settings.py you set 'ENGINE': 'django.db.backends.postgresql_psycopg2' this settings is for PostgreSQL not for MySQL. You must change this string to folowing: 'ENGINE': 'django.db.backends.mysql' .

Hope this is help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM