Hello I have a post blog where I have a homepage with posts and if you click on it you are redirected to the post_detail page. Now I want the users to allow to delete their comments which I achieved but I want them to stay on the same post_detail page. I could not come over it and hope that someone can help me. Thanks in advance.
post_detail.html
<div class="container">
<br>
<h1 style="text-align:center;">{{post.post}} </h1>
<p style="font-size:small;text-align:center;">{{post.created_on}}</p>
<button type="button" class="btn btn-secondary float-right" data-toggle="modal" data-target="#exampleModal" data-whatever="#mdo">Yorum At</button>
<br>
<br>
<br>
{% for i in commentmodel_list%}
<h4 >{{i.comment}}</h4>
{% if i.author_security == user%}
<a style="float:right;color:red;"href="{% url 'forum:delete' slug=post.slug comment_id=i.id %}"><i class="fas fa-minus-circle"></i></a>
{% endif %}
{% if i.author is none %}
<p style="font-size:small;">Anonim | {{i.created_on}}</p>
{% else%}
<p style="font-size:small;"><a style="text-decoration:none;" href="#">#{{i.author}}</a> | {{i.created_on}}</p>
{% endif %}
<hr>
<br>
{% endfor %}
</div>
urls.py
app_name='forum'
urlpatterns=[
path('', views.PostList.as_view(), name='post'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('delete/<slug:slug>/<comment_id>/',views.delete_post,name='delete')
]
views.py
def delete_post(request,slug=None,comment_id=None):
comment_delete=Comment.objects.get(id=comment_id)
comment_delete.delete()
post = Post.objects.get(slug=slug)
return redirect('forum:post_detail', slug= post)
I would highly recommend to delete the comments via submitting a POST request with CSRF token, otherwise it's prone to CSRF attack. When you'll be submitting the form, you can add there an input with a value representing the current page which you'll redirect to afterwards. Pseudocode:
<form action="{% url 'forum:delete' slug=post.slug comment_id=i.id %}"
method="POST">
{% csrf_token %}
<input type="hidden" name="current_post" value="{{ post.slug }}">
<input type="submit>
</form>
view:
def delete_post(request, comment_id=None):
Comment.objects.filter(id=comment_id).delete()
return redirect('forum:post_detail', slug= request.POST['current_post'])
It's not obvious if by "I want them to stay on the same post_detail page" you mean that you dont want to perform the redirect in browser and you'd rather delete it via ajax. If that's the case, please check the Django documentation about AJAX request with CSRF, as there's an example how to do exactly that.
Related
So, what I am using it for is to render a template that I have.
Here is the code it has a problem with.
`
#views.route('/')
def home():
return render_template("home.html")
`
By the way this is the code in home.html
{% extends "base.html" %} {% block title %}Home{% endblock %} {% block content
%}
<h1 align="center">Notes</h1>
<ul class="list-group list-group-flush" id="notes">
{% for note in user.notes %}
<li class="list-group-item">
{{ note.data }}
<button type="button" class="close" onClick="deleteNote({note,id })"
<span aria-hidden="true">×</span>
</button>
</li>
{% endfor %}
</ul>
<form method="POST">
<textarea name="note" id="note" class="form-control"></textarea>
<br />
<div align="center">
<button type="submit" class="btn btn-primary">Add Note</button>
</div>
</form>
{% endblock %}
I run it. It gives me a link to website. Everything there, No. I run it. It gives me a link to website. And then jinja2.exceptions.UndefinedError: 'user' is undefined is on my website
Check your html file, you may accidently got a variable called user in there
I have a small Django application. I am setting up authentication and I have just finished the forgotten/reset password functionality. However, once the password is reset it does not redirect to the success page. I've trawled through all my code and cannot find any references to 'login' that is incorrectly coded. The error that appears is the following: 'Reverse for 'login' not found. 'login' is not a valid view function or pattern name'
Here's the relevant files from my project:
account urls.py
from django.urls import path, include, reverse_lazy
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
#Login URLs
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
#User Pages URLs
path('dashboard/', views.dashboard, name='dashboard'),
path('nodes/', include('nodes.urls')),
#Password Changes URLs
path('password_change/', auth_views.PasswordChangeView.as_view(success_url=reverse_lazy('account:password_change_done')), name='password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
#Password Reset URLs
path('password_reset/',
auth_views.PasswordResetView.as_view(success_url=reverse_lazy('account:password_reset_done')),
name='password_reset'),
path('password_reset/done/',
auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(success_url=reverse_lazy('account:password_reset_complete')),
name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
password_reset_confirm.html:
{% block content %}
<div class="page-title">
<h1>Reset Password</h1>
</div>
<br>
<div class="login-container">
<div class="login-box">
<br>
<h3 style="text-align: center;">Reset your password</h3>
<div class="login-form" style="text-align: center; ">
{% if validlink %}
<p>Please enter your new password twice:</p>
<form method="post">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Change my password"/></p>
</form>
{% else %}
<p>The password reset link was invalid, possibly because it has
already been used. Please request a new password reset link</p>
{% endif %}
</form>
</div>
</div>
</div>
{% endblock %}
password_reset_done.html
{% block content %}
<div class="page-title">
<h1>Password Reset Confirmation</h1>
</div>
<br>
<div class="login-container">
<div class="login-box">
<br>
<h4 style="text-align: center;">An email has been sent to the email associated with your account, follow the link
in the email to reset your password.</h4>
<h4>If you do not receive an email, please try again and make sure you have entered the correct email</h4>
</div>
</div>
</div>
{% endblock %}
I have had NoReverseMatch errors in the past and have managed to fix them, but I'm a bit stumped with this one
Any help would be great!
Your current reset password functionality expects a url with the name login. You've defined the login view in an app that has an app_name of "account". This creates a namespace for all of the urls in that module. So your login view has a name of account:login.
Do you happen to have settings.LOGIN_URL set to "login"? Something is likely calling reverse("login") or {% url "login" %}. Changing that to "account:login" will fix it or moving your login view up to the root out of the account namespace will work.
I have a Django method written to update user profile. The purpose of the method is solved, as I am able to click on the "update" button and modify the existing data.
Note: I have written method to update Default User model and extended User model(custom fields). Below is the code snippet from views
views.py
#login_required(login_url="/login/")
def editUserProfile(request):
if request.method == "POST":
form = UserProfileUpdateForm(request.POST, instance=request.user) # default user profile update
obj = UserProfile.objects.get(user__id=request.user.id)
form1 = UserProfileForm(request.POST or None, instance=obj)
if form.is_valid() and form1.is_valid():
obj.Photo = form1.cleaned_data['Photo']
obj.dob = form1.cleaned_data['dob']
obj.country = form1.cleaned_data['country']
obj.State = form1.cleaned_data['State']
obj.District = form1.cleaned_data['District']
obj.phone = form1.cleaned_data['phone']
form.save()
form1.save()
messages.success(request, f'updated successfully')
return redirect('/profile1')
else:
messages.error(request, f'Please correct the error below.')
else:
form = UserProfileUpdateForm(instance=request.user)
form1 = UserProfileUpdateForm(instance=request.user)
return render(request, "authenticate\\editProfilePage.html", {'form': form, 'form1': form1})
corresponding HTML code.
editProfilePage.html
{% load static %}
{% block content %}
<h2 class="text-center">Edit Profile</h2>
<form method="POST" action="{% url 'editUserProfile' %}">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-warning alert-dismissable" role="alert">
<button class="close" data-dismiss="alert">
<small><sup>x</sup></small>
</button>
<p>Form has error..!!</p>
{% for field in form %}
{% if field.errors %}
{{ field.errors }}
{% endif %}
{% endfor %}
</div>
{% endif %}
{{ form.as_p }}
{{ form1.as_p }}
<!-- {{ form1.dob }}-->
<!-- {{ form1.country }}-->
<!-- {{ form1.State }}-->
<!-- {{ form1.District }}-->
<!-- {{ form1.phone }}-->
<input type="submit" value="update" class="btn btn-secondry">
</form>
<br/><br/>
{% endblock %}
If one see in the first glance I do not see any issue immediately as my purpose of updating the profile is successful.
However, to test, after I update a user profile I logout the user which redirects me to login page, and there I see error "Please correct the error below." three times which is coming from the "else" part of the update method. I also see a message "updated successfully" on the same login screen which is coming from the "if" part of the update method as mentioned above(screenshot attached -- update_error3).
So, I have below observations:
My update method "editUserProfile" is somehow calling the inner most "if - else" together.
I think the issue lies in the HTML page. I could say this because when I click on the "update" button from the profile page, I see "email" field appearing twice on the screen and the "update button to insert the data to database(screenshot attached -- update_error1).
Now, I could see the rest of the fields only after I click on the "update" button further(screenshot attached -- update_error2). Furthermore, this is when I enter all my details and when I click on the "update" button, the data get saved successfully in the database and redirected to the profile page with the new data.
Finally, I think the issue is something related to the HTML code. May be I am wrong.
Any thought?
update_error1
The problem is with your messages rendering on the template.Just add this block of code
to your base template that immediately extends your update template.
{% if messages %}
<ul class="messages ">
{% for message in messages %}
<ol class="breadcrumb ">
<li{% if message.tags %} class="{{ message.tags }} " {% endif %}><strong>{{ message }} {{form.errors}}</strong>
</li>
</ol>
{% endfor %}
</ul>
{% endif %}
I have the following html that the button does a normal Save:
<form action="" method="post">
<input type="hidden" value="{{ people }}" name="people">
<input type="hidden" value="{{ number }}" name="number">
<p>{% trans 'Are you sure you want to save people' %} {{ people }} {% trans 'where number is' %} {{ number }}?
</p>
{% buttons %}
<button class="btn btn-default hover-linea" id="cancel" type="button">
{% bootstrap_icon "remove" %} {% trans 'Cancel' %}
</button>
<button type="submit" class="btn btn-default hover-linea">
{% bootstrap_icon "log-out" %} {% trans 'Save' %}
</button>
{% endbuttons %}
What I need when I do the Save, is to reload the previous page to the form. I want to press Save and do the same as now, but also make a location.reload() of a page whose relative link is (../people.html).
I await an answer, thank you very much.
Your server-side form handler (which you have specified the URL to in your action) attribute should respond with an HTTP redirect (status 302 and the Location: header) to the URL you want the browser to load.
Very new to Shopify and wondered if anyone could help me. I've some products listed that when clicked should provide a link to an affiliate as opposed to the add to cart button. To create the affiliate option I used Metafield Solid to add the affiliate link data:
Key:url
Namespace: affiliate
In the themes (Minimal) I made the following changes:
<!-- Affiliate metafield contains? {{ product.metafields.affiliate.url }} -->
<div class="purchase-section{% if product.variants.size > 1 %} multiple{% endif %}">
<div class="purchase">
{% if product.metafields.affiliate.url %}
<a target="_blank" href="{{ product.metafields.affiliate.url }}" />Buy from Affiliate</a>
{% else %}
<!-- this would be your original button -->
<input type="submit" id="add-to-cart" class="btn" name="add" value="{{ 'products.general.add_html' | t | escape }}" />
{% endif %}
When I preview the page it still shows the Add to cart button, yet when I view the source the comment containing product.metafields.affiliates.url clearly shows the correct affiliate URL. Is there something obvious I'm missing?