如何使用ForeignKey主题更新Django中的页面

时间:2018-10-11 03:46:04

标签: python django django-forms updates

我是django的新手,我正在尝试更新一个带有外键主题的页面。这是我的代码

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>

当我尝试保存表单时,视图将不会重定向,页面也不会更新,请您启发我如何完成页面更新任务。

预先感谢

2 个答案:

答案 0 :(得分:0)

您尝试过

def my_view(request):
    ...
    return redirect('/some/url/')
  

引用https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/

答案 1 :(得分:0)

尝试打印从子弹中获得的文章,如果要获得该文章,则只需分配

query = _context.VpAbcIndex .AsNoTracking() .Select(e => new VpAbcIndex { Id = e.Id, ParentName = e.ParentName ?? "!", ParentEndDate = e.ParentEndDate, ParentStartDate = e.ParentStartDate, ParentCode = e.ParentCode, ParentNote = e.ParentNote ?? "", ParentStatus = e.ParentStatus, ChildName = e.ChildName ?? "!", ChildId = e.ChildId, ChildEndDate = e.ChildEndDate, ChildStartDate = e.ChildStartDate, CodeEnd = (filter.Level == 0) ? goods .FirstOrDefault( x => x.Code == e.ParentCode).Enddate : (filter.Level == 1) ? goods .FirstOrDefault(x => x.Code == e.ChildCode).Enddate : (filter.Level == 2) ? goods .FirstOrDefault(x => x.Code == e.GrandChildCode).Enddate : null }) ; }

现在您将拥有一个带有以前值的初始化表单。

另一个建议,尝试使用django类的基本视图。仍然有任何疑问对此发表评论。