django.urls.exceptions.NoReverseMatch on deleteview - python

I am deleting a post using generic DeleteView. On clicking the link for delete it returns
django.urls.exceptions.NoReverseMatch: Reverse for 'postdelete' with
no arguments not found. 1 pattern(s) tried:
['posts/(?P<id>[0-9]+)/delete/$']
I tried placing the before and after /delete/
#urls.py
path('posts/<int:id>/delete/',blogpostDelete.as_view(),name='postdelete'),
#DeleteView
class blogpostDelete(DeleteView):
success_url='/posts/'
template_name="blog/delete.html"
def get(self,request,id) :
Blogpost = get_object_or_404(blogpost,id=id)
return self.render_to_response({'id':id})
#link in template
<a href={% url "postdelete" id=id %}>Delete</a>
#delete.html
{% block content %}
<form action={% url "postdelete" %} method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ id }}"?</p>
<input type="submit" value="Confirm">
</form>
{% endblock %}

You're not passing id to the url.
Try something like this
<form action={% url "postdelete" id="{{id}}" %} method="post">

In Delete View No need to pass id in html form action
Doing Like this:
<form method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ id }}"?</p>
<input type="submit" value="Confirm">
</form>

Related

LIKE button in a Django project

There is a view in the Django project (a paginated blog) that is responsible for how likes work. It has one drawback: when a user likes a post, it is redirected to the main page of the site. How can I fix this so that the user would remain on the page where they liked.
views.py
class AddLikeView(View):
def post(self, request, *args, **kwargs):
blog_post_id = int(request.POST.get('blog_post_id'))
user_id = int(request.POST.get('user_id'))
url_from = request.POST.get('url_from')
user_inst = User.objects.get(id=user_id)
blog_post_inst = News.objects.get(id=blog_post_id)
try:
blog_like_inst = BlogLikes.objects.get(blog_post=blog_post_inst, liked_by=user_inst)
except Exception as e:
blog_like = BlogLikes(blog_post=blog_post_inst,
liked_by=user_inst,
like=True)
blog_like.save()
return redirect(url_from)
template.py
<form action="{% if not is_liked_bool %}{% url 'add' %}{% else %}{% url 'remove' %}{% endif %}" method="post">
{% csrf_token %}
<input type="hidden" name="blog_post_id" value="{{ blog_post_id }}">
<input type="hidden" name="user_id" value="{% if user.is_authenticated %}{{ request.user.id }}{% else %}None{% endif %}">
<input type="hidden" name="url_from" value="{{ request.path }}">
{% if is_liked_bool %}
<input type="hidden" name="blog_likes_id" value="{{ blog_likes_id }}">
{% endif %}
<button type="submit" class="btn btn-success">
{% if not is_liked_bool %}
<i class="fi-xnluxl-heart">♥</i>
{% else %}
<i class="fi-xnluxl-heart-solid">♥</i>
{% endif %}
<span class="likes-qty">{{ likes_counter }}</span>
</button>
I think you should check the url_from field first. Just print it and if it's wrong, you should change the {{request.path}} field in your template.
You can try this:
{{ request.get_full_path }}
And also if I remember correctly, you can access the path with request.path in your view and no need to send path via template.

Django password reset issue

I saw a tutorial and I implemented a password resetter through email. The email sending part works fine and I get the email after clicking it I get redirected to reset password page. But after I give the new password and click Submit it gets redirected to the login page but the password is not getting reset.
urls.py
path('password-reset/',auth_views.PasswordResetView.as_view(template_name='password_reset.html'),name='password_reset'),
path('password-reset/done/',auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html'),name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html'),name='password_reset_confirm'),
path('password-reset-complete/',auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html'),name='password_reset_complete'),
password_reset_confirm.py
{% extends "base.html" %}
{% load static %}
{% block head_block %}
<link rel="stylesheet" type="text/css" href="{% static 'index.css' %}">
{% endblock head_block %}
{% block body_block %}
{% csrf_token %}
<form method="post" action="{% url 'reg_sign_in_out:user_login' %}">
{% csrf_token %}
<input type="password" name="" id="">
<input type="submit" value="Reset">
</form>
{% endblock %}
Any idea where the problem is?
I think that you have to put in your form the action of your post, for example if you have to go to the password-reset/done/ URL, you have to add
<form method="post" action="password-reset/done/" >
{% csrf_token %}
<input type="password" name="" id="">
<input type="submit" value="Reset">
</form>
Because if you don't put the action, it will redirect to the same page
The problem was a redirecting issue. Its fixed.

Invalid Endblock tag in django

I am receiving an error message when I go to one of my pages in my Django project, as it is saying that the End-block tag is invalid (asks whether I remembered to register or load). The error looks like this:
My code for this template - (login.html) - is below:
{% extends "learning_logs/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.'</p>
{% endif %}
<form method="post" action="{% url 'users:login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name="sumbit">log in</button>
<input type="hidden" name="next" value="{% extends 'learning_logs/index.html' %}" />
</form>
{% endblock content %}
I am very confused, and I am wondering whether anyone knows what the problem is?
Thanks
Milo
<input type="hidden" name="next" value="{% extends 'learning_logs/index.html' %}" />
Above line contains {% extends .... %}. To prevent being interpreted as a extends tag, use templatetag tag:
<input type="hidden" name="next" value="{% templatetag openblock %} extends 'learning_logs/index.html' {% templatetag closeblock %}" />
use {% endblock %} instead o f {% endblock content %}
for me it was wrongly auto-formatted html file. Check auto-formatting for html files.

Django password is not changed

I don't know what I'm doing wrong, but when I try to change the password it doesn't change it and it doesn't give any errors.
urls.py
from django.contrib.auth.views import logout,password_change,password_change_done
...
url(r'^change_password/?$',password_change, name='password_change'),
url(r'^password_changed/?$',password_change_done, name='password_change_done'),
url(r'^logout/?$',logout, name='logout'),
password_change_form.html
<form action="{% url 'password_change_done' %}" method="post">
{% csrf_token %}
{% bootstrap_form form layout="inline" form_group_class="form-group col-md-6" %}
<div class="clearfix"></div>
{% buttons %}
<button type="submit" name="save" class="btn btn-primary">{% bootstrap_icon "plus" %} {% trans 'save' %}</button>
{% endbuttons %}
</form>
When I click on save, it shows the template "password_change_done.html" but the password hasn't been changed. However, there are no errors in the console and I don't know what fails.
Thank you all
The form action should be password_change not password_change_done:
<form action="{% url 'password_change' %}" method="post">
Django will automatically do the redirect to password_change_done for you once the password change is successful.

Setup POST HTTP Request to create variable .vnc file

I have a database with users information and IP's. What I would like to do is to dynamically create, and then open, a *.vnc file with their IP.
In my views.py file I have this:
def view_list(request):
template_name = 'reader/list.html'
cust_list = xport.objects.all()
#if request.method == 'POST':
#<a href=link to created *.vnc file>Connect to client</a>
return render(request, template_name, {'xport': cust_list})
The commented out portion is just what I've been playing with and what I currently think I need to do.
My template file is list.html and looks like this:
{% extends "base.html" %}
{% load url from future %}
{% block content %}
<h1> Customer List </h1>
<ul>
{% for c in xport %}
{% if c.ip %}
<li>{{ c.firstName }} {{ c.lastName }}</li>
<form method="POST" action=".">{% csrf_token %}
<input type="submit" name="submit" value="Create VNC to {{ c.ip }}" />
</form>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
What I would like to do is to click on the "Create VNC" button and then that create and open the *.vnc file.
This should give you an idea:
url(r'^file/vnc/$', 'myapp.views.vnc', name='vnc-view'),
views.py
from django.views.decorators.http import require_POST
#require_POST
def vnc(request):
ip = request.POST.get('ip', None)
response = HttpResponse(ip, content_type='application/octet-stream')
# If you don't want the file to be downloaded immediately, then remove next line
response['Content-Disposition'] = 'attachment; filename="ip.vnc"'
return response
template
<form method="POST" action="{% url 'vnc-view' %}">{% csrf_token %}
<input type="hidden" name="ip" value="127.0.0.1" />
<input type="submit" name="submit" value="Create VNC to 127.0.0.1" />
</form>

Categories