[英]How To Solve “No Reverse Match” With Django
What I am trying to do is allow for a user to click on a product, be taken to the product page and then be able to add a review to that product page.我想要做的是允许用户单击产品,被带到产品页面,然后能够向该产品页面添加评论。 The reviews are appended to the product as users are adding the reviews.
当用户添加评论时,评论会附加到产品中。 I'm not sure if this is the best approach to use.
我不确定这是否是最好的使用方法。
I have been running into issues with this error for the last few days after editing a form.在编辑表单后的最后几天,我一直遇到此错误的问题。 Please let me know if you have any solutions.
如果您有任何解决方案,请告诉我。
Thank You!谢谢你!
Models.py模型.py
class Beach(models.Model):
name = models.CharField(max_length=80)
location = models.CharField(max_length=200)
video = models.FileField(upload_to='beachvideo', blank=True)
beachPic = models.ImageField(default='default.jpg', upload_to='beachphotos', blank=True)
datetimeInfo = models.DateTimeField(auto_now=True)
lat = models.FloatField()
lon = models.FloatField()
info = models.TextField()
def average_rating(self):
all_ratings = map(lambda x: x.rating, self.review_set.all())
return np.mean(all_ratings)
def __str__(self):
return f'{self.name}'
class Review(models.Model):
RATING = (
('1', 'Avoid'),
('2', 'Not Great'),
('3', 'Decent'),
('4', 'Awesome'),
('5', 'The Best'),
)
beach = models.ForeignKey(Beach, on_delete= models.CASCADE)
author = models.ForeignKey(User, null=True, blank=True, on_delete= models.CASCADE)
ratingRank = models.CharField(max_length= 100, blank=False, choices=RATING)
waveIntensityRank = models.CharField(max_length= 100, blank=True, choices=RATING)
crowdednessRank = models.CharField(max_length= 100, blank=True, choices=RATING)
pollutionRank = models.CharField(max_length= 100, blank=True, choices=RATING)
noiseLevelRank = models.CharField(max_length= 100, blank=True, choices=RATING)
servicesRank = models.CharField(max_length= 100, blank=True, choices=RATING)
comments = models.TextField(max_length=250, blank=True)
pub_date = models.DateTimeField(auto_now=True)
def get_absolute_url(self):
return reverse('beaches-home')
def __str__(self):
return self.author.username
Views.py视图.py
def beachView(request):
beachView = {
'beachView': Beach.objects.all()
}
return render(request, 'beaches/index.html', context = beachView)
def beachDescription(request, pk):
beachDesc = {
'beachDesc': Beach.objects.get(pk=pk)
}
return render(request, 'beaches/description.html', context = {'beachDesc': beachDesc} )
def review_list(request):
latest_reviews = Review.objects.order_by('-pub_date')[:9]
def add_review(request, beach_id):
beachRev = get_object_or_404(Beach, pk=beach_id)
review_form = ReviewForm(request.POST)
if review_form.is_valid():
rating = review_form.cleaned_data['rating']
comment = review_form.cleaned_data['comments']
user_name = review_form.cleaned_data['author']
wave_intensity = review_form.cleaned_data['waveIntensityRank']
crowds = review_form.cleaned_data['crowdednessRank']
pollution = review_form.cleaned_data['pollutionRank']
noise_level = review_form.cleaned_data['noiseLevelRank']
services = review_form.cleaned_data['servicesRank']
review = Review()
review.beach = beachRev
review.author = user_name
review.rating = rating
review.waveIntensityRank = wave_intensity
review.crowdednessRank = crowds
review.pollutionRank = pollution
review.noiseLevelRank = noise_level
review.servicesRank = services
review.comments = comment
review.pub_date = datetime.datetime.now()
review.save()
return HttpResponseRedirect(reverse('beaches/description.html', args=(beach_id,)))
#if request.method == "POST":
#review_form = review_form(data=request.POST)
#if review_form.is_valid():
# rating = review_form.cleaned_data
# post.date = timezone.now()
# post.save()
# return redirect('beaches/beachReview.html')
#else:
# form=ReviewForm()
return render(request, 'beaches/beachReview.html', context= {'beachRev': beachRev, 'review_form': review_form})
class searchResultsView(ListView):
model = Beach
template_name = 'beaches/searchResults.html'
def get_queryset(self): # new
query = self.request.GET.get('q')
object_list = Beach.objects.filter(
Q(name__icontains=query) | Q(location__icontains=query)
)
return object_list
HTML HTML
{% extends 'base.html' %}
{% load static %}
{% block content %}
<head>
<link rel="stylesheet" href="{% static 'css/bestbeach.css' %}">
</head>
<h1> {{ beachDesc.name }} </h1>
<h4> Location: {{ beachDesc.location }} </h4>
<h4> Rating: {{ beachDesc.ratingRank }} </h4>
<table>
<tr>
<td> <img src="{{ beachDesc.beachPic.url }}" width="350px" height="200px"/> </td>
</tr>
<tr>
<td> <video width="350px" height="200px" loop="loop" preload="auto" controls>
<source src="{{ beachDesc.video.url }}" type="video/mp4">
Your browser does not support the video.
</video>
</td>
</tr>
</table>
<h4> Information: {{ beachDesc.info }} </h4>
{% if user.is_authenticated %}
<a href="{% url 'beaches-review' %}">
<button class="goButton" type="submit">Leave a Review</button>
</a>
{% endif %}
{% if review_list %}
<div>
{% for r in review_list %}
<div>
<h4> Reviews: </h4>
<p> {{ r.author }} </p>
<h6> rated {{ r.rating }} of 5 </h6>
<p>{{ r.comments }}</p>
</div>
{% endfor %}
</div>
{% else %}
<p>No reviews are available.</p>
{% endif %}
{% endblock %}
URLS.py URLS.py
urlpatterns = [
path('', views.beachView, name='beaches-home'),
path('<int:pk>/', views.beachDescription, name='beaches-description'),
path('search/', views.searchResultsView.as_view(), name='search-results'),
path('review/<int:pk>', views.add_review, name='beaches-review')
]
Thanks for any help!谢谢你的帮助!
It looks like you are trying to generate the url using a template rather than a route name:看起来您正在尝试使用模板而不是路由名称生成 url:
return HttpResponseRedirect(reverse('beaches/description.html', ...))
Instead you want:相反,你想要:
return HttpResponseRedirect(reverse("beaches-description", ...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.