0

I have forms to Post Article in my blog Django2. When I run django server there is no error, but when I Post and submit Article in my frontEnd apps nothing happens and doesn't save any data.

Any help on this would be highly appreciated!

Template HTML

<div class="create-article">
    <h2>Create an Awesome New Articles</h2>
    <form class="site-form" action="{% url 'articles:create' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form }}
        <input type="submit" value="Create">
    </form>
</div>

forms.py

from django import forms
from . import models

class CreateArticle(forms.ModelForm):
class Meta:
    model = models.Article
    fields = ['title', 'body', 'slug', 'thumb']

views.py

def article_create(request):
if request.method == 'POST':
    form = forms.CreateArticle(request.POST, request.FILES)
    if form.is_valid():
        # save article to db
        instance = form.save(commit=False)
        instance.author = request.user
        instance.save
        return redirect('articles:list')
else:
    form = forms.CreateArticle()
return render(request, 'articles/article_create.html', {'form': form})

urls.py

from django.urls import path
from . import views

app_name = 'articles'

urlpatterns = [
    path('', views.article_list, name="list"),
    path('create/', views.article_create, name="create"),
    path('<slug>/', views.article_detail, name="detail"),
]

Thanks.

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65

1 Answers1

0

It starts here: instance = form.save(commit=False), where you are not commiting the save.

Further down you have instance.save as if you were setting a model field, but with not value given to it.

Make that line instance.save() instead.