无法通过表单发送pk参数从模板发送到Django中的视图,因此无法使用Form-Action编辑我的帖子

时间:2019-12-08 17:50:47

标签: django

我有公告对象。 我可以保存新的公告,但是在编辑时,我想查看具有标题和内容的现有公告,然后将同一公告对象的更改保存在同一页面上。

模板:Announcements.html

{% for Announcement in announcements %}
    <div id="{{ Announcement.pk }}" style="display: none">

                        <div id = "editform" style=" display: flex; flex-direction: row ">
                            <div style="flex: 100%;">
                                <form action={% url 'edit_announcement' %} id="{{ Announcement.pk }}" method="POST" class="post-form">{% csrf_token %}
                                    <dl>
                                        <dt><input type="text" value="{{ Announcement.title }}" name="title" placeholder="Title" style="width:100%"> </dt>
                                        <br>
                                        <dd><textarea rows="5" name="content" placeholder="Content" style="width:100%">{{ Announcement.content }}</textarea></dd>
                                    </dl>

                                </form>
                            </div>

                            <div style="flex:content; min-width: fit-content;text-align:center; margin-left: 5px" >
                                <dt><button type="submit" form="{{ Announcement.pk }}" onclick="editForm()" style="width:65px">Save</button></dt>
                                <br>

                            </div>

                        </div>
                    </div>

似乎是这样的: https://imgur.com/a/w9Tublr 当我单击编辑按钮时,如下所示: https://imgur.com/a/raHmK2Y

views.py:

def edit_announcement(request, pk=None):
    announcement = get_object_or_404(Announcement, pk=pk)
    if request.method == "POST":
        form = AnnouncementForm(request.POST, instance=announcement)
        if form.is_valid():
            announcement = form.save(commit=False)
            announcement.author = request.user
            announcement.created_date = timezone.now()
            announcement.save()
            return redirect('/Announcements/', pk=announcement.pk)
        else:
            form = AnnouncementForm(instance=announcement)
        return render(request, '/Announcements', {'form': form})

js:

function editForm(x) {
            var x = document.getElementById(x);

            if (window.getComputedStyle(x).display === "none") {
                x.style.display = "block";
            }
            else{
                x.style.display = "none";
            }
        }

urls.py

urlpatterns = [
    path('edit_announcement/', views.edit_announcement, name='edit_announcement'),
]

forms.py

class AnnouncementForm (forms.ModelForm):
    class Meta:
        model = Announcement
        fields = ('title', 'content')

models.py

class Announcement(models.Model):
    title = models.CharField(max_length=1000, null=True)
    content = models.CharField(max_length=1000, null=True)
    lecture = models.ForeignKey(Lecture, on_delete=models.CASCADE, blank=True, null=True)
    created_date = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)

    def saveAnnouncement(self):
        self.author = self.request.user
        self.created_date = timezone.now()
        self.save()

    def __str__(self):
        x = str(self.title)
        y = str(self.content)
        xy = (x + '\n' + y)
        return xy

    def get_announcement(self):
        return reverse("announcement", kwargs={"pk": self.pk})

0 个答案:

没有答案