我一直在收到这个错误,我似乎无法修复它。 以下是它的屏幕截图:erros image
这里我的观点是.py:'
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from posts.forms import PostForm, CommentForm
from django.core.urlresolvers import reverse_lazy
from django.http import Http404
from django.views import generic
from braces.views import SelectRelatedMixin
from . import forms
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
class PostList(SelectRelatedMixin, generic.ListView):
model = models.Post
select_related = ("user", "group")
class UserPosts(generic.ListView):
model = models.Post
template_name = "posts/user_post_list.html"
def get_queryset(self):
try:
self.post_user = User.objects.prefetch_related("posts").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.post_user.posts.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["post_user"] = self.post_user
return context
class PostDetail(SelectRelatedMixin, generic.DetailView):
model = models.Post
select_related = ("user", "group")
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(
user__username__iexact=self.kwargs.get("username")
)
class CreatePost(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
# form_class = forms.PostForm
fields = ('message','group')
model = models.Post
# def get_form_kwargs(self):
# kwargs = super().get_form_kwargs()
# kwargs.update({"user": self.request.user})
# return kwargs
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
class DeletePost(LoginRequiredMixin, SelectRelatedMixin, generic.DeleteView):
model = models.Post
select_related = ("user", "group")
success_url = reverse_lazy("posts:all")
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id=self.request.user.id)
def delete(self, *args, **kwargs):
messages.success(self.request, "Post Deleted")
return super().delete(*args, **kwargs)
def add_comment_to_post(request, pk):
post = get_object_or_404(models.Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('posts:single', pk=post.pk)
else:
form = CommentForm()
return render(request, 'posts/comment_form.html', {'form': form})
def comment_approve(request, pk):
comment = get_object_or_404(Comment, pk=pk)
comment.approve()
return redirect('posts:single', pk=comment.post.pk)
def comment_remove(request, pk):
comment = get_object_or_404(Comment, pk=pk)
post_pk = comment.post.pk
comment.delete()
return redirect('posts:single', pk=post_pk.pk)
这是我的models.py:
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
import misaka
from groups.models import Group
from django.contrib.auth import get_user_model
User = get_user_model()
class Post(models.Model):
user = models.ForeignKey(User, related_name="posts")
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name="posts",null=True, blank=True)
def __str__(self):
return self.message
def save(self, *args, **kwargs):
self.message_html = misaka.html(self.message)
super().save(*args, **kwargs)
def approve_comments(self):
return self.comments.filter(approved_comment=True)
def get_absolute_url(self):
return reverse(
"posts:single",
kwargs={
"username": self.user.username,
"pk": self.pk
}
)
class Meta:
ordering = ["-created_at"]
unique_together = ["user", "message"]
class Comment(models.Model):
post = models.ForeignKey('posts.Post', related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def get_absolute_url(self):
return reverse("post_list")
def __str__(self):
return self.text
这是我的post_detail.html:
<!DOCTYPE html>
{%load staticfiles%}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="footer, address, phone, icons" />
<link href="https://fonts.googleapis.com/css?family=Abel|Raleway|Signika|Signika+Negative" rel="stylesheet">
<title>Ask Uptown</title>
<style media="screen">
* {
margin: 0;
padding: 0;
}
body{
font-family: 'Abel', sans-serif;
background-color: #E6ECF0;
}
.h12{
text-align: center;
}
.hero{
position: relative;
width: 1200px;
margin-left: 500px;
margin-top: 0;
top: 190px;
}
.banner{
color: white;
font-size: 40px;
text-align: center;
position: relative;
top: 170px;
left: 125px;
}
.buttons{
margin-top: 15px;
margin-left: 490px;
}
.btn{
border: 1px solid white;
padding: 10px 30px;
color: white;
text-decoration: none;
}
.buttons a:hover{
background-color: #cdc9c9;
transition: all 0.5s ease-in;
}
.about{
width: 100%;
height: 300px;
color: #F4F7F8;
}
.about h1{
color: black;
margin-top: 60px;
margin-left: 2px;
font-family: 'Signika Negative', sans-serif;
font-weight: 500;
font-size: 35px;
}
.paragraph-about{
color:black;
font-family: 'Signika Negative', sans-serif;
margin-left: 70px;
margin-right: 60px;
margin-top: 30px;
}
.section{
width: 100%;
height: 350px;
background-color: #F4F7F8;
}
.card-pic{
background-color: #fff;
width: 300px;
height: 300px;
position: relative;
display: inline-block;
margin-top: 30px;
margin-right: 30px;
margin-left: 40px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
border-radius: 10px;
}
.card-pic h4 {
text-align: center;
font-size: 25px;
margin-top: 20px;
}
.card-pic p{
text-align: center;
margin-left: 20px;
margin-right: 12px;
}
.card-pic-two{
background-color: #fff;
width: 300px;
height: 300px;
position: relative;
display: inline-block;
margin-left: 100px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
border-radius: 10px;
}
.card-pic-two h4{
text-align: center;
font-size: 25px;
margin-top: 20px;
}
.card-pic-two p{
text-align: center;
margin-left: 20px;
margin-right: 12px;
}
.card-pic-three{
background-color: #fff;
width: 300px;
height: 300px;
position: relative;
display: inline-block;
margin-left: 150px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
border-radius: 10px;
}
.card-pic-three h4{
text-align: center;
font-size: 25px;
margin-top: 20px;
}
.card-pic-three p{
text-align: center;
margin-left: 20px;
margin-right: 12px;
}
.main-nav {
float: right;
list-style: none;
margin-top: 45px;
}
.main-nav li {
display: inline-block;
}
.main-nav li a {
color: white;
text-decoration: none;
padding: 5px 20px;
font-family: 'Signika', sans-serif;
font-size: 19px;
margin-right: 10px;
}
.main-nav li:hover a {
border: 1px solid white;
}
.main-nav li a:active {
border: 1px solid white;
}
.footer-distributed{
background-color: #292c2f;
box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.12);
box-sizing: border-box;
width: 100%;
text-align: left;
font: bold 16px sans-serif;
padding: 55px 50px;
margin-top: 80px;
}
.footer-distributed .footer-left,
.footer-distributed .footer-center,
.footer-distributed .footer-right{
display: inline-block;
vertical-align: top;
}
/* Footer left */
.footer-distributed .footer-left{
width: 40%;
}
/* The company logo */
.footer-distributed h3{
color: #ffffff;
font: normal 36px 'Cookie', cursive;
margin: 0;
}
.footer-distributed h3 span{
color: #5383d3;
}
/* Footer links */
.footer-distributed .footer-links{
color: #ffffff;
margin: 20px 0 12px;
padding: 0;
}
.footer-distributed .footer-links a{
display:inline-block;
line-height: 1.8;
text-decoration: none;
color: inherit;
}
.footer-distributed .footer-company-name{
color: #8f9296;
font-size: 14px;
font-weight: normal;
margin: 0;
}
/* Footer Center */
.footer-distributed .footer-center{
width: 35%;
}
.footer-distributed .footer-center i{
background-color: #33383b;
color: #ffffff;
font-size: 25px;
width: 38px;
height: 38px;
border-radius: 50%;
text-align: center;
line-height: 42px;
margin: 10px 15px;
vertical-align: middle;
}
.footer-distributed .footer-center i.fa-envelope{
font-size: 17px;
line-height: 38px;
}
.footer-distributed .footer-center p{
display: inline-block;
color: #ffffff;
vertical-align: middle;
margin:0;
}
.footer-distributed .footer-center p span{
display:block;
font-weight: normal;
font-size:14px;
line-height:2;
}
.footer-distributed .footer-center p a{
color: #5383d3;
text-decoration: none;;
}
/* Footer Right */
.footer-distributed .footer-right{
width: 20%;
}
.footer-distributed .footer-company-about{
line-height: 20px;
color: #92999f;
font-size: 13px;
font-weight: normal;
margin: 0;
}
.footer-distributed .footer-company-about span{
display: block;
color: #ffffff;
font-size: 14px;
font-weight: bold;
margin-bottom: 10px;
}
.footer-distributed .footer-icons{
margin-top: 25px;
}
.footer-distributed .footer-icons a{
display: inline-block;
width: 35px;
height: 35px;
cursor: pointer;
background-color: #33383b;
border-radius: 2px;
font-size: 20px;
color: #ffffff;
text-align: center;
line-height: 35px;
margin-right: 3px;
margin-bottom: 5px;
}
.footer-distributed{
position: relative;
top: 200px;
}
/* If you don't want the footer to be responsive, remove these media queries */
@media (max-width: 880px) {
.footer-distributed{
font: bold 14px sans-serif;
}
.footer-distributed .footer-left,
.footer-distributed .footer-center,
.footer-distributed .footer-right{
display: block;
width: 100%;
margin-bottom: 40px;
text-align: center;
}
.footer-distributed .footer-center i{
margin-left: 0;
}
}
.twitter{
width: 42px;
height: 42px;
}
.facebook{
width: 42px;
height: 42px;
}
.instagram {
width: 40px;
height: 40px;
}
.icons{
margin-top: 20px;
}
.adress{
width: 30px;
height: 30px;
margin-right: 40px;
}
.email{
width: 40px;
height: 40px;
}
.phone{
width: 20px;
height: 20px;
}
.adress-sec{
margin-bottom: 25px;
}
.phone-sec{
margin-bottom: 20px;
}
.logo-text{
color: #ffffff;
font: normal 36px 'Cookie', cursive;
margin: 0;
margin-top: 30px;
margin-left: 27px;
float: left;
text-decoration: none;
font-size: 27px;
}
.logo-text span{
color: #5383d3;
}
.backgr{
background-color: #7F7F7F;
height: 13vh;
width: 100%;
background-size: cover;
background-position: center;
}
header{
background: src(.backgr);
}
.no-ans{
position: relative;
top: 50px;
left: 50px;
font-size: 20px;
}
.add-comment{
position: relative;
text-decoration: none;
z-index: 99;
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 40px;
background: linear-gradient(45deg, #B388EB, #8093F1);
border-radius: 20px;
color: #FFF;
font-size: 20px;
letter-spacing: 1px;
font-weight: 200;
left: 1100px;
text-decoration: none;
left: 800px;
top: 75px;
}
</style>
</head>
<body>
<header>
<div class="backgr">
<nav class="" role="navigation" id="navbar">
<div class="">
<div class="logo">
<a class="logo-text" href="{% url 'home' %}"><h3>Uptown<span>Ask</h3></span></a>
</div>
<ul class="main-nav">
{% if user.is_authenticated %}
<li class="active"><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'posts:create' %}">Stuck? Ask A question</a></li>
<li><a href="{% url 'groups:all' %}">Groups</a></li>
<li><a href="{% url 'posts:for_user' username=user.username %}">My Post History</a></li>
<li><a href="{% url 'accounts:logout' %}">Log out</a></li>
{% else %}
<li class="active"><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'groups:all' %}">Groups</a></li>
<li><a href="{% url 'accounts:login' %}" >Log in</a></li>
<li><a href="{% url 'accounts:signup' %}" >Sign up</a></li>
<li><a href="#">Contact</a></li>
{% endif %}
</ul>
</header>
<section>
{% block post_content %}
<div class="col-md-8">
{% include "posts/_post.html" %}
</div>
{% endblock %}
<a class="add-comment" href="{% url 'posts:add_comment_to_post' pk=post.pk%}">Answer This Question</a>
</div>
{% for comment in post.comments.all %}
<br>
{% if user.is_authenticated or comment.approved_comment %}
{{ comment.created_date }}
{% if not comment.approved_comment %}
<a class="btn btn-default" href="{% url 'comment_remove' pk=comment.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
<a class="btn btn-default" href="{% url 'comment_approve' pk=comment.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
{% endif %}
<p>{{ comment.text|safe|linebreaks }}</p>
<p>Posted by: <strong>{{ comment.author }}</strong></p>
{% endif %}
{% empty %}
<p>No comments posted.</p>
{% endfor %}
</div>
</section>
<footer class="footer-distributed">
<div class="footer-left">
<h3>Uptown<span>Ask</span></h3>
<p class="footer-links">
<a href="#">Home</a>
·
<a href="#">Blog</a>
·
<a href="#">About</a>
·
<a href="#">Contact</a>
</p>
<p class="footer-company-name">Ask Uptown © 2017</p>
</div>
<div class="footer-center">
<div class="adress-sec">
<p class="p-adress">Address: Tripoli Street, Algeria Road, Mirdif Area - Dubai, United Arab Emirtes</p>
</div>
<div class="phone-sec">
<p>Phone: 04 251 5001</p>
</div>
<div class="email-sec">
<p class="p-email"><a href="mailto:rayanza3eem1234@gmail.com">Email: Uptownschool@gmail.com</a></p>
</div>
</div>
<div class="footer-right">
<p class="footer-company-about">
<span>About the company</span>
Lorem ipsum dolor sit amet, consectateur adispicing elit. Fusce euismod convallis velit, eu auctor lacus vehicula sit amet.
</p>
<div class="icons">
<a href="https://www.twitter.com" target="_blank"><img src="images/twitter.png" alt="Twitter" class="twitter" ></a>
<a href="https://www.facebook.com" target="_blank"><img src="facebook.png" alt="Facebook" class="facebook" ></a>
<a href="https://www.instagram.com" target="_blank"><img src="insta.png" alt="Instagram" class="instagram" ></a>
</div>
</div>
</footer>
</body>
</html>
我似乎无法在django中找到此高级错误的修复程序。任何人都可以帮忙。
我一直试图解决它,但不能,每个评论都被认为是与评论的链接。
由于
答案 0 :(得分:1)
错误指向
return redirect('posts:single', pk=post.pk)
可能导致错误,因为除了username
之外,它还希望您传递pk
参数。