Horai Nuri
Horai Nuri

Reputation: 5578

Django - Form is not saving the modifications

I'm creating a page where my users can edit their articles but there is one problem, my form is not saving the modifications when I submit my form. Let's have a look:

views.py

def edit(request, id=None, template_name='article_edit.html'):

    if id:
        article = get_object_or_404(Article, id=id)
        if article.user != request.user:
            return HttpResponseForbidden()
    else:
        article = Article(user=request.user)

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            save_it = form.save()
            save_it.save()
            form.save_m2m()
            return HttpResponseRedirect("/")
    else:
        form = ArticleForm(instance=article)

    context = {'form': form}
    populateContext(request, context)
    return render(request, template_name, context)

line 3 to 8 : Is to check if it's your own articles there is no problem with that.

line 10 to 18 : There is a problem between these lines, my form is not saving when I submit my form

forms.py (ModelForm)

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        exclude = ['date', 'rating', 'user']

form = ArticleForm()

(Line 4 : I'm excluding the date, the ratings and the username when saved so that the user can not change these informations.)

I don't have any problem in my template.html and the urls.py don't bother with that I checked over 10 times :)

Any help would be great, I'm blocked on this problem since over 1 month...


******* EDIT *******

Models.py

class Article(models.Model):
    user = models.ForeignKey(User)
    titre = models.CharField(max_length=100, unique=True)
    summary = RichTextField(null=True, max_length=140)
    contenu = RichTextField(null=True)
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution")
    image = models.ImageField(upload_to='article', default='article/amazarashi.jpeg')
    rating = RatingField(can_change_vote=True)
    tags = TaggableManager(through=TaggedItem, blank=True)

    def __str__(self):
        return self.titre

Template.html

{% block body %}

{% if user.is_authenticated %}
      <p>Authentificated as <strong>{{ user.username }}</strong></p>
    {% else %}
      <p>NOT Authentificated</p>
    {% endif %}

<h1>Edit this {{ article.titre }}</h1>

<br>

<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
<form enctype='multipart/form-data' action="{% url "article.views.index" %}" method="post" class="form"  autocomplete="off" autocorrect="off">
    {% csrf_token %}

    <div class="form-group">TITRE
      {{ form.titre.errors }}
      {{ form.titre }}
    </div>
    <div class="form-group">SUMMARY
      {{ form.media }}
      {{ form.summary.errors }}
      {{ form.summary }}
    </div>
    <div class="form-group">CONTENU
      {{ form.media }}
      {{ form.contenu.errors }}
      {{ form.contenu }}
    </div>
    <div class="form-group">
      {{ form.image.errors }}
      {{ form.image }}
   </div>
   <div class="form-group">TAGS
      {{ form.tags.errors }}
      {{ form.tags }}
   </div>

   <input type="submit" class="btn btn-default" value="Submit"/>
</div>

</form>

{% endblock %}

Upvotes: 0

Views: 214

Answers (1)

bellum
bellum

Reputation: 3710

According to your error message.. Modify your form in templates:

<form enctype='multipart/form-data' ...>

Add request.FILES to your Form creation:

if request.POST:
    form = ArticleForm(request.POST, request.FILES, instance=article)

UPDATE: You have strange url tag parameter. Are you sure you have defined your url like: ('...', your_view, name='article.views.index')? I am in doubt cause url tag takes url name as parameter but not view path and nobody usually uses url names like this.

Upvotes: 2

Related Questions