I'm new to django, I'm trying to update a page with foreignkey to subject. Here is my code
models.py
class Subject(models.Model):
title = models.CharField(max_length = 200)
slug = models.SlugField(blank = False)
visible = models.BooleanField(default = True)
position = models.IntegerField()
date_published = models.DateTimeField(auto_now_add = True)
date_updated = models.DateTimeField(auto_now_add=False)
def __str__(self):
return self.title
class Page(models.Model):
subject = models.ForeignKey(Subject, on_delete= models.CASCADE)
title = models.CharField(max_length = 200)
slug = models.SlugField(blank = False)
description = models.TextField()
thumbs = models.ImageField(blank = True)
visible = models.BooleanField(default = True)
position = models.IntegerField()
date_published = models.DateTimeField(auto_now_add = True, auto_now=False)
date_updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __str__(self):
return self.title
urls.py
urlpatterns = [
path('dashboard/', views.dashboard, name='dashboard'),
re_path(r'^dashboard/edit/(?P<subject_slug>[\w-]+)/(?P<slug>[\w-]+)', views.edit_article, name='edit_page'),
]
views.py
def edit_article(request, subject_slug, slug):
subject = Subject.objects.get(slug = subject_slug)
article = subject.page_set.get(slug = slug)
form = ArticleForm(request.POST or None, instance = article)
if request == request.POST:
form = ArticleForm(request.POST, instance = article)
if form.is_valid():
c = form.save( commit = False)
c = subject
c.save()
return HttpResponseRedirect('/dashboard/')
context = {
'title' : article.title,
'article' : article,
'form':form
}
return render(request, 'dashboard/editarticle.html', context)
forms.py
from django import forms
from .models import Subject, Page, NewsFeed
class ArticleForm(forms.ModelForm):
class Meta:
model = Page
fields = ['title','slug','description','thumbs', 'visible','position']
article.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Edit article</title>
<link rel="stylesheet" href="">
</head>
<body>
<h1>{{ title }}</h1>
<form method="POST" accept-charset="utf-8">{% csrf_token %}
{{ form.as_p }}
<input type ='submit' value='submit'/>
</form>
</body>
</html>
When I'm trying to save the form the views wont redirect and the page wont update, can you guys enlighten me on how to achieved the task of updating the page.
Thanks in advance
Did you try
def my_view(request):
...
return redirect('/some/url/')
Reference https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/
Try to print the article that you are getting from slug, if it is getting the article then simply assign
form = ArticleForm(instance = article)
Now you will have a initialized form with previous values.
One more suggestion, try to use django class base view. Still have any doubts comment it.
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.