One field form - do i need Form class
I want to have search form with just one field. Should I use Form class or is it overkill? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFyHfE2D4kUYygEMPujTv-WFyjedtf_drB7304_aayUeMQMTEA%40mail.gmail.com.
Django +Ajax + jQuery problem
My problem is that site registers only the first click on .upvote or .downvote element and ignores next ones. {% extends 'base.html' %} {% load votes_exists %} {% block title %}{{question.title|truncatechars:52}}{% endblock %} {% block content %} {{question.title}} {{question.content}} {% for answer in question.answers.all %} {{answer.author}} {{answer.content}} {% votes_up_exists answer request.user.id as is_upvoted %} {% votes_down_exists answer request.user.id as is_downvoted %} ↑ {{answer.vote_score}} ↓ {% endfor %} {% csrf_token %} {{form.as_p}} {% endblock %} {% block javascript %} {% load static %} https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js "> //upvote and downvote script var csrftoken = window.Cookies.get('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); $(".upvote").click( function() { let answerid = $(this).data("answer-id"); $.post("{% url 'upvote' %}", {answer_id:answerid}, function(response) { /*$("#score-" + answerid).load(window.location.href + " " + "#score-" + answerid); $("#upvote-" + answerid).load(window.location.href + " " + "#upvote-" + answerid); $("#downvote-" + answerid).load(window.location.href + " " + "#downvote-" + answerid);*/ $("#answer-" + answerid).load(window.location.href + " " + "#answer-" + answerid); alert(response); }); }); $(".downvote").click( function() { let answerid = $(this).data("answer-id"); $.post("{% url 'downvote' %}", {answer_id:answerid}, function(response) { /* $("#score-" + answerid).load(window.location.href + " " + "#score-" + answerid); $("#upvote-" + answerid).load(window.location.href + " " + "#upvote-" + answerid); $("#downvote-" + answerid).load(window.location.href + " " + "#downvote-" + answerid);*/ $("#answer-" + answerid).load(window.location.href + " " + "#answer-" + answerid); alert(response); }); }); {% endblock %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFyHfE3_EogRCb-2xvGpS6ACDNPbF_JxBLzsfD%3D2XLoBKsJysA%40mail.gmail.com.
Custom template tags - instance.templatetag
Hi! How to change my template tag? from django import template register = template.Library() @register.simple_tag def votes_up_exists(answer, user_id): pass how I use it - {% votes_up_exists answer request.user.id %} how I would like to use it - {% answer.votes_up_exists user_id %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c0aba2c4-b13d-4ba2-9478-d32661c81307%40googlegroups.com.
DJango upvoting, downvoting - functions in templates
I'm using this package https://pypi.org/project/django-vote/ and i want to do something smilar to this guy https://github.com/shellfly/django-vote/issues/51 - I need function callable in template, which will tell me if user has downvoted or upvoted, but exactly downvoted or exactly upvoted. I don't know where should I put it's code to make it work. It should take two arguments: user_id and answer. My view: def question(request, question_id): question = get_object_or_404(Question, pk=question_id) form = AnswerForm() return render(request, 'questions/question.html', {'question': question, 'form' : form},) I'd like to use this function here with upvote, downvote elements.: {% for answer in question.answers.all %} {{answer.author}} {{answer.content}} ↑ {{answer.votes.count}} ↓ {% endfor %} Also I'm not sure which functions can be called in Django templates. Only those having no arguments? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/050550b7-65db-4e08-9b22-c367f7b2a71f%40googlegroups.com.
Ajax and Django and jQuery - .attr()
I have a problem with Ajax and Django templates. I'm new to Ajax and jquery. Console log at the end of the script prints undefined. I don't know why let answerid = $(this).attr("answer-id"); doesn't extract attribute from this line: ↑ I also want to know if using template tags in javascript scripts like here $ .post("{% url 'upvote' %}", {answer_id:answerid}) is a good idea. {% extends 'base.html' %} {% block title %}{{question.title|truncatechars:52}}{% endblock %} {% block content %} {{question.title}} {{question.content}} {% for answer in question.answers.all %} {{answer.author}} {{answer.content}} ↑ {{answer.votes.count}} ↓ {% endfor %} {% csrf_token %} {{form.as_p}} {% endblock %} {% block javascript %} {% load static %} https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js";> //upvote and downvote script var csrftoken = window.Cookies.get('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); $(".upvote").click( () => { let answerid = $(this).attr("answer-id"); console.log(answerid); $.post("{% url 'upvote' %}", {answer_id:answerid}); }); {% endblock %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aa129ea5-dd3d-402f-a297-796b408e8217%40googlegroups.com.
Re: Redirect to login page without losing content of form
Ok. Now I've dealt with this returned None problem, but I still have no idea what to do to keep my form content after clicking submit button. Does next parmeter generated by login_required contain only link or also form content? W dniu środa, 27 maja 2020 15:58:22 UTC+2 użytkownik Jan Gregorczyk napisał: > > *I* have problem similar to this one: > https://stackoverflow.com/questions/50272631/django-form-data-lost-on-making-login-required-on-post > I want answer to be added if user is logged in otherwise redirect user to > login page, let him login and then add his answer. My problem is that I > lose content of the form. > At the moment I'm getting this errror: > > The view questions.views.answer didn't return an HttpResponse object. It > returned None instead. > > > def question(request, question_id): > question = get_object_or_404(Question, pk=question_id) > form = AnswerForm() > return render(request, 'questions/question.html', {'question': question, > 'form' : form},) > > @login_required > def answer(request, question_id): > question = get_object_or_404(Question, pk=question_id) > form = AnswerForm(request.POST) if request.method == 'POST' else > AnswerForm() > if form.is_valid(): > answer = form.save(commit=False) > answer.author = request.user > answer.question = question > answer.save() > return HttpResponseRedirect(reverse('question', args=(question.id,))) > > > > > {% csrf_token %} > {{form.as_p}} > > > > form method="post" action="{% url 'login' %}"> > {% csrf_token %} > {{ form.as_p }} > > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cd4abaec-face-4f8f-8a69-72258fe611b1%40googlegroups.com.
Redirect to login page without losing content of form
*I* have problem similar to this one: https://stackoverflow.com/questions/50272631/django-form-data-lost-on-making-login-required-on-post I want answer to be added if user is logged in otherwise redirect user to login page, let him login and then add his answer. My problem is that I lose content of the form. At the moment I'm getting this errror: The view questions.views.answer didn't return an HttpResponse object. It returned None instead. def question(request, question_id): question = get_object_or_404(Question, pk=question_id) form = AnswerForm() return render(request, 'questions/question.html', {'question': question, 'form' : form},) @login_required def answer(request, question_id): question = get_object_or_404(Question, pk=question_id) form = AnswerForm(request.POST) if request.method == 'POST' else AnswerForm() if form.is_valid(): answer = form.save(commit=False) answer.author = request.user answer.question = question answer.save() return HttpResponseRedirect(reverse('question', args=(question.id,))) {% csrf_token %} {{form.as_p}} form method="post" action="{% url 'login' %}"> {% csrf_token %} {{ form.as_p }} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4d2bd93d-3a92-4e14-9b8b-f758616c6cfb%40googlegroups.com.