Answer the question
In order to leave comments, you need to log in
User feedback, how to do it right?
Hello.
Situation:
There is a Course model (course) - there is a Creview model (Course Review) - There is a standard User model (user). The task is to make sure that authorized users can leave their reviews.
How I did:
models.py
class Course(models.Model):
....
class Creview(models.Model):
creview = models.ForeignKey(Course, on_delete=models.CASCADE, default=1)
userview = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
review = models.TextField(help_text="Отзыв", verbose_name="Сам отзыв о курсе")
class CreviewForm(forms.Form):
review = forms.CharField(widget=forms.Textarea, label='Ваш отзыв', required=False)
<form action="/addcreview{{ courseitem.id }}" method="post">
{% csrf_token %}
<input type="hidden" name="userfromform" value="{{ user.id }}">
{{ form.as_p }}
<input type="submit" class="expanded button" value="ОПУБЛИКОВАТЬ">
</form>
def addcreview(request, add_id):
token = csrf(request)
if request.POST:
course = Course.objects.get(id = add_id)
user_id = request.POST.get('userfromform', '')
user = User.objects.get(id = user_id)
form = CreviewForm(request.POST)
if form.is_valid():
review = form.cleaned_data['review']
review_obj = Creview(creview=course, userview=user, review=review)
review_obj.save()
return redirect('/course'+ add_id, token)
Answer the question
In order to leave comments, you need to log in
Of course something is wrong. Look at the names of classes and attributes.
And also transfer the user in a hidden field ... So you can write reviews from other people. You get the user through request.user
models.py
class Creview(models.Model):
...
def get_absolute_url(self):
return reverse('course_review', kwargs={'pk': self.id})
...
url(r'^creview/(?P<pk>\d+)/$', DetailCourseReview.as_view(), name='course_review'),
url(r'^creview/(?P<course_id>\d+)/create/$', login_required(CreateCourseReview.as_view()), name='create_course_review'),
...
class CourseReviewForm(forms.ModelForm):
class Meta:
model = Creview
fields = ['review']
def __init__(self, user, course, *args, **kwargs):
super().__init__(*args, **kwargs)
self.instance.creview = course
if user and user.is_authenticated():
self.instance.userview = user
class DetailCourseReview(DetailView):
model = Creview
class CreateCourseReview(CreateView):
model = Creview
form_class = CourseReviewForm
template_name = 'course_review.html'
def dispatch(self, request, *args, **kwargs):
course_id = kwargs.get('course_id')
self.course = get_object_or_404(Course, pk=course_id)
super().dispatch(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
kwargs['course'] = self.course
return kwargs
<form action="{% url "create_course_review" course_id=course.id %} method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="{% trans "Send" %}">
</form>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question