在我的论坛之类的网站上。我想要一个功能,如果用户是组的一部分,则只有用户可以选择在组中发布。现在,当我单击“ POST”按钮时,我试图在URL中传递组子词。但由于某些原因,URL不匹配。分别有两个用于群组和帖子的应用程序。
{% extends "groups/group_base.html" %}
{% block content %}
<h1>{{group.name}}</h1>
<h2> Member Count: {{group.members.count}}</h2>
<div class="content">
{% if user in group.members.all %}
<a href="{% url 'groups:leave' slug=group.slug %}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-remove-circle"></span> Leave</a>
//line 10 <a href="{% url 'posts:create1' slug=group.slug %}" class="btn btn-lg btn-fill btn-warning" ><span class="glyphicon glyphicon-ok-circle"></span>Post</a></li>
{% else %}
<a href="{% url 'groups:join' slug=group.slug %}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-ok-circle"></span> Join</a>
{% endif %}
</div>
from django.conf.urls import url
from django.urls import path
from . import views
app_name='posts'
urlpatterns = [
url(r"^$", views.PostList.as_view(), name="all"),
url(r"new/in/(?P<slug>[-\w]+)/$", views.CreatePost.as_view(), name="create1"),
url(r"by/(?P<username>[-\w]+)/$",views.UserPosts.as_view(),name="for_user"),
url(r"by/(?P<username>[-\w]+)/(?P<pk>\d+)/$",views.PostDetail.as_view(),name="single"),
url(r"delete/(?P<pk>\d+)/$",views.DeletePost.as_view(),name="delete"),
]
# from django.shortcuts import render
# # Create your views here.
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.http import Http404
from django.views import generic
from django.shortcuts import get_object_or_404
from groups.models import Group
from braces.views import SelectRelatedMixin
from . import forms
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
class CreatePost(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
# form_class = forms.PostForm
fields = ('message',)
model = models.Post
def get(self, request, *args, **kwargs):
group = get_object_or_404(Group,slug=self.kwargs.get("slug"))
# try:
# GroupMember.objects.create(user=self.request.user,group=group)
# except IntegrityError:
# messages.warning(self.request,("Warning, already a member of {}".format(group.name)))
# else:
# messages.success(self.request,"You are now a member of the {} group.".format(group.name))
return super().get(request, *args, **kwargs)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.group=group
self.object.save()
return super().form_valid(form)
{% extends "posts/post_base.html" %}
{% load bootstrap3 %}
{% block content %}
<h4>Create New Post</h4>
//line 7 <form method="POST" action="{% url 'posts:create1' slug=group.slug %}" id="postForm">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Post" class="btn btn-primary btn-large">
</form>
{% endblock %}
点击发布按钮时出现以下错误- error image
答案 0 :(得分:0)
该错误来自注释后的邮政应用程序的“ post_form.html”第7行。
从-
进行更改<form method="POST" action="{% url 'posts:create1' slug=group.slug %}" id="postForm">
到-
<form method="POST" action="" id="postForm">
我通过在网页上进行打印进行了检查,即使url显示了正确的子弹,group.slug仍以这种形式变为空,因此网页{{group.slug}}上没有打印任何内容。
这解决了以上错误。
我还更改了postapp的views.p的createPost视图,以使我的功能正常工作。 在此过程中,我到处都将“ slug”更改为“ pk”,以使用主键引用该组。
因此,上述所有文件现在看起来都如下所示([创建帖子功能现在可以正常运行]-
{% extends "groups/group_base.html" %}
{% block content %}
<h1>{{group.name}}</h1>
<h2> Member Count: {{group.members.count}}</h2>
<div class="content">
{% if user in group.members.all %}
<a href="{% url 'groups:leave' slug=group.slug %}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-remove-circle"></span> Leave</a>
//line 10 <a href="{% url 'posts:create1' pk=group.pk %}" class="btn btn-lg btn-fill btn-warning" ><span class="glyphicon glyphicon-ok-circle"></span>Post</a></li>
{% else %}
<a href="{% url 'groups:join' slug=group.slug %}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-ok-circle"></span> Join</a>
{% endif %}
</div>
from django.conf.urls import url
from django.urls import path
from . import views
app_name='posts'
urlpatterns = [
url(r"^$", views.PostList.as_view(), name="all"),
url(r"new/in/(?P<pk>\d+)/$", views.CreatePost.as_view(), name="create1"),
url(r"by/(?P<username>[-\w]+)/$",views.UserPosts.as_view(),name="for_user"),
url(r"by/(?P<username>[-\w]+)/(?P<pk>\d+)/$",views.PostDetail.as_view(),name="single"),
url(r"delete/(?P<pk>\d+)/$",views.DeletePost.as_view(),name="delete"),
]
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.http import Http404
from django.views import generic
from django.shortcuts import get_object_or_404
from groups.models import Group
from braces.views import SelectRelatedMixin
from . import forms
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
class CreatePost(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
fields = ('message',)
model = models.Post
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.group=get_object_or_404(Group,pk=self.kwargs.get('pk'))
self.object.save()
return super().form_valid(form)
{% extends "posts/post_base.html" %}
{% load bootstrap3 %}
{% block content %}
<h4>Create New Post</h4>
//line 7 <form method="POST" action="" id="postForm">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Post" class="btn btn-primary btn-large">
</form>
{% endblock %}