When I click "Logout" button,Page not found error is raised.
views.py:
#login_required
def logout_view(request):
logout(request)
return render(request, 'registration/accounts/top.html')
in index.html
<div class="container">
<ul class="top-menu">
<form name="logout" method="post" action="{% url 'accounts:logout_view' %}">
<button type="submit" class="btn btn-primary btn-lg" style="color:white;background-color: #F62459;border-style: none;">Logout</button>
<input name="next" type="hidden"/>
</form>
</ul>
</div>
But I put Logout button,Page not found (404) error happens.Traceback is
Page not found (404)Request Method: POST
Request URL: http://localhost:8000/static/accounts/logout_view
My ideal system is when I put logout button, it sends top.html.
project tree
accounts
|migrations
|static
|templates
|registration
|accounts
|base.html
|detail.html
What is wrong in my code?How should I fix this?
urls.py is
urlpatterns = [
url(r'^top$', views.top,name='top'),
url(r'^detail$', views.detail,name='detail'),
url(r'^login/$', login,
{'template_name': 'registration/accounts/login.html'},
name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
]
You name the logout url 'logout':
url(r'^logout/$', views.logout_view, name='logout'),
Therefore you should also refer to it as 'logout' when reversing the url:
<form name="logout" method="post" action="{% url 'accounts:logout' %}">
Related
I am trying to build an inventory management with Django.
When building delete option, I am encountering an error.
The error message is "Reverse for 'delete' not found. 'delete' is not a valid view function or pattern name."
I will paste my code below.
Delete function code:
def delete(request, iid):
obj = inventory.objects.get(id=iid)
obj.delete()
return render(request, 'main/lists.html')
HTML Template code:
<a class="btn btn-danger bg-gradient" href="{% url 'delete' i.id %}" role="button">Delete</a>
Urls.py code:
urlpatterns = [
path('', views.index, name="index"),
path('lists', views.lists, name="lists"),
path('add', views.add, name="add"),
path('edit', views.edit, name="edit"),
path('delete/<int:id>', views.delete, name="delet") ]
Please give me a valid solution.
In urls.py the url pointing to the method is called delet.
Update it to delete on urls.py or to delet on the HTML template.
From the docs you can do this:
{% url 'delet' i.id as the_url %}
<a class="btn btn-danger bg-gradient" href="{{ the_url }}" role="button">Delete</a>
I know similar kind of question is asked before, but I was not able to get it. This is my first project as a Django beginner.
In my Django blog app, I made a delete button but it is not working and I am finding for answers, trying different methods on the web but it did not help.
I am trying to do is when admin open the post, then on clicking the delete button, it take the post-id and delete that post and redirect to home page, but it is not working as expected. So, lastly I came here. Any help will be appreciated. Thanks!
This is my urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('post/<int:pk>', views.post, name='post'),
path('about', views.about, name='about'),
path('contact_us', views.contact_us, name='contact_us'),
path('register', views.register, name='register'),
path('login', views.login, name='login'),
path('logout', views.logout, name='logout'),
path('create_post', views.create_post, name='create_post'),
path('delete_post', views.delete_post, name='delete_post')
]
This is my views.py file:
def delete_post(request, *args, **kwargs):
pk = kwargs.get('pk')
post = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
post.delete()
return redirect('/')
return render(request, 'delete-post.html')
This is delete post html form:
<form action="{% url 'delete_post' post.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete post">
</form>
Delete button:
<button type="button" class="btn btn-danger" style="position:relative; right: -1145px;">Delete</button>
for deleting a post
def delete_post(request, id):
post = Post.objects.filter(id=id)
address.delete()
return redirect('/')
and in your html
<a class="btn btn-outline-danger" href="{% url 'appname:delete_post' id=post.id %}">Delete It</a>
and in your urls.py
path('<int:id>/delete-post',views.delete_post,name='delete_post')
So I have probably the simplest task you could ever wish for with an html form that I cannot solve.
I want to input a brand name into a search bar, click submit and it redirect to the url/brand/[input]
I already have a working view for the urls setup in django. How would I structure the form to actually create my desired url?
<form method="GET" action="">
<input type="text" name="brand" id="brand" placeholder="Search..." aria-label="Search">
<button type="submit">Search</button>
</form>
views.py
class GetByBrand(ListView):
def get(self, request, brand):
urls.py
urlpatterns = [
path('', GetListView.as_view(), name='home'),
path('brands/<str:brand>/', GetByBrand.as_view())
]
<form method="GET" action="brands/">
<input type="text" name="brand" id="brand" placeholder="Search..." aria-label="Search">
<button type="submit">Search</button>
in URLs(import views by from. import views)
urlpatterns = [
path('brands/', views.GetByBrand),
]
in Views
from django.http import HttpResponse
def GetByBrand(request):
s = request.GET['brand']
return HttpResponse(s)
OR use AJAX in Javascript in your HTML file
$('#brand').on('submit',function(e){
var brand = $('input[name=brands]')
$.get("brands/"+brand, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
})
So I basically solved this by using the code I had previously tried which is identical to what Wagas Develper recommended. What I had not done though was add this last line to the urls.py in the main project folder. (and I'm still not sure why this solved it but found the solution from throwing crap at the wall)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('shoes.urls')),
path('brands/', include('shoes.urls'))
For anyone curious about how the class based view is structured now.
class GetByBrand(ListView):
def get(self, request, brand):
get_results = request.GET.get('q')
The urls.py in the same directory as views.py (within app dir not main project dir)
urlpatterns = [
path('', GetListView.as_view(), name='home'),
path('brands/<str:brand>/', GetByBrand.as_view()),
path('brand_results/', GetByBrand.as_view(), name='b')
]
HTML Form
<form method="GET" class="form-inline my-2 my-lg-0" action="{% url 'b' %}">
<input class="form-control mr-sm-2 search-btn" type="text" name="q" value="{{request.GET.q}}" placeholder="Search..." aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
Im new to Django (also to stackoverflow too) and im trying to make simple accounts system.
When im "sign out" button to main page im getting this error
Reverse for 'logout' not found. 'logout' is not a valid view function or pattern name.
account/views.py
def signout_view(request):
if request.method == 'POST':
logout(request)
return redirect('/')
account/urls.py
app_name = 'account'
urlpatterns = [
path('signup/', views.signup_view, name='signup'),
path('signin/', views.signin_view, name='signin'),
path('signout/', views.signout_view, name='signout'),
]
home/templates/home/wrapper.html
<form class="logout-link" action="{% url 'account:logout' %}" method="post">
{% csrf_token %}
<button type="submit">Sign out</button>
</form>
anyone can help with fixing this problem?
reverse function gets the name stated at the end of the path funtion in urls.py and retrieves its url
path('signup/', views.signup_view, name='signup'),
here the name is 'signup' and reverse('signup') will return a 'signup/'. Jinja2's {% url %} tag just calls reverse in django
so to make thing work in your code
just do
<form class="logout-link" action="{% url 'account:signout' %}" method="post">
{% csrf_token %}
<button type="submit">Sign out</button>
</form>
I'm fairly new to django and I'm currently rewriting the django login views so I can move all the authentication process to a dedicated django app where I can redefine the templates.
So currently I have an accounts app which url.py looks like that:
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.login, {'template_name': 'accounts/login.html'}, name='login'),
path('logout/', auth_views.logout, name='logout'),
path('password_reset/', auth_views.password_reset,
{'template_name': 'accounts/password_reset_form.html',
'email_template_name': 'accounts/password_reset_email.html',
'subject_template_name': 'accounts/password_reset_subject.txt',
'post_reset_redirect': 'done/'}, name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(
template_name='accounts/password_reset_done.html'), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.password_reset_confirm,
{'template_name': 'accounts/password_reset_confirm.html',
'post_reset_redirect': '/accounts/reset/done/'}, name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(
template_name='accounts/password_reset_complete.html'), name='password_reset_complete')
]
And a accounts/password_reset_email.html which looks like this:
{% autoescape off %}
To initiate the password reset process for your {{ user.get_username }} account,
click the link below:
{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}?origin_page={{ request.POST.origin_page }}"
...
Sincerely,
The Team
{% endautoescape %}
What I want to do is to recover the origin_page argument so when the user click on the reset link from the email he get redirected to the right webpage after his password is reset. So far I tried to do this in password_reset_form.html:
{% block content %}
<p>Forgotten your password? Enter your email address below, and we'll email instructions for setting a new one.</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="hidden" name="origin_page" value="{{ request.GET.origin_page }}"/>
<button type="submit">Submit</button>
</form>
{% endblock %}
Here I just want to pass in my origin_page to the submit form so that I can retrieve it from accounts/password_reset_email.html, the url for this page looks like this http://127.0.0.1:8000/accounts/password_reset/?origin_page=/mypage/.
How can I recover the request.POST.origin_page parameter from accounts/password_reset_email.html?
Thanks.
maybe that helps
path('password-reset/',
auth_views.PasswordResetView.as_view(
template_name='form.html',
email_template_name='email.html',
extra_email_context={
'origin_page':'origin_page_thing'
}
),
name='password_reset'),
email:
{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}?origin_page={{ origin_page }}
E-Kami solution:
Ok I've got it, I actually created a new PasswordResetView like this: ' \
class PasswordResetView(auth_views.PasswordResetView):
template_name = 'commons/password_reset_form.html'
email_template_name = 'commons/password_reset_email.html'
subject_template_name = 'commons/password_reset_subject.txt'
success_url = 'done/'
def post(self, request, *args, **kwargs):
self.extra_email_context = {
'origin_page': self.request.POST['origin_page']
}
return super().post(request, *args, **kwargs)