Hi Everyone I made A Blog but when i delete post i get this error i'm using django latest version:
TypeError at /post/(?P3\d+)/remove/
'str' object is not callable'
Here Is My Views.py
class PostDeleteView(LoginRequiredMixin,DeleteView):
success_url = reverse_lazy('post_list')
model = Post
Here is my app Urls.py
from django.urls import path
from blog import views
urlpatterns = [
path('',views.PostListView.as_view(),name='post_list'),
path('about/',views.AboutView.as_view(),name='about'),
path('register/',views.user_register,name='user_register'),
path('post/(?P<pk>\d+)',views.PostDetailView.as_view(),name='post_detail'),
path('post/new/',views.CreatePostView.as_view(),name='post_new'),
path('post/(?P<pk>\d+)/edit/',views.PostUpdateView.as_view(),name='post_edit'),
path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),
path('drafts/',views.PostDraftListView.as_view(),name='post_draft_list'),
path('post/(?P<pk>\d+)/comment/',views.add_comment_to_post,name='add_comment_to_post'),
path('comment/(?P<pk>\d+)/approve/',views.comment_approve,name='comment_approve'),
path('comment/(?P<pk>\d+)/remove/',views.comment_remove,name='comment_remove'),
path('post/(?P<pk>\d+)/publish/',views.post_publish,name='post_publish'),
]
Image
https://i.stack.imgur.com/wbcfn.png
Here is my post_confirm_delete.html
{% extends "blog/base.html" %}
{% block content %}
<form method="POST">
{% csrf_token %}
<p>Are You sure you want to delete {{ object }}?</p>
<input type="submit" class="btn btn-danger" value="Confirm">
</form>
{% endblock %}
Here is my Project Urls.py
"""blogpro URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth.views import LoginView,LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('accounts/login/', LoginView.as_view(), name='login'),
path('accounts/logout/', LogoutView.as_view(), name='logout', kwargs={'next_page': '/'}),
]
You are using path with regex which is not how it work docs, only use regex url string with re_path
it should be :
path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),
Related
I am Working on my project and I don't know how this error I got
Can anybody see what I'm missing?
This is my root project urls.py
from django.contrib import admin
from django.urls import path, include
from .import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('onlinePizza/', include('onlinePizza.urls')),
path('cart/', include(('cart.urls'), namespace='cart')),
path('accounts/', include(('accounts.urls'), namespace='accounts')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my cart app urls.py
from django.urls import path
from .import views
from cart.views import AddCart
app_name = 'cart'
urlpatterns = [
path('add_cart/', views.AddCart, name='Cart'),
]
This is my cart app views.py
from django.shortcuts import render, redirect
from cart.models import *
def AddCart(request):
product = request.POST.get('product')
remove = request.POST.get('remove')
cart = request.session.get('cart')
if not cart:
request.session['cart'] = {}
if cart:
quantity=cart.get(product)
if quantity:
if remove:
if quantity<=1:
cart.pop(product)
else:
cart[product]=quantity-1
else:
cart[product]=quantity+1
else:
cart[product]=1
else:
cart={}
cart[product]=1
request.session['cart']=cart
return redirect ('Menu')
This is my onlinePizza/pc_menu.html template
<form action="{% url 'cart:AddCart' %}" method="POST">{% csrf_token %}
<input hidden type="text" name="product" value="{{i.id}}">
<button class="main-btn cart cart-btn" style="padding: 5px 32px">Add <i class="fa-solid fa-cart-shopping"></i></button>
</form>
I try to solve this problem, but I show this error everywhere in my project.
You need that name:
urlpatterns = [
path('add_cart/', views.AddCart, name='Cart'),
]
and that link generator:
{% url 'cart:AddCart' %}
refering to the exactly same value. So it has to be either 'Cart' and 'cart:Cart' or 'AddCart' and 'cart:AddCart'.
I'm trying to import a simple single-field form in Django, and I have gone through plenty of Tutorial Videos on YouTube describing the same. Yet, I'm unable to render the simple form on my web-app. I'm pretty sure I'm doing something really stupid that is still oblivious to me.
I'll also post in the folder structure so you can suggest if I'm defining the class/function in the wrong views.py file.
The appropriate source codes are as follows:
earthquake/views.py File
from django.shortcuts import render
from earthquake.forms import HomeForm
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'earthquake/home.html'
def get(self, request, *args, **kwargs):
form1 = HomeForm()
argf = {
'myform': form1
}
return render(request, self.template_name, argf)
forms.py
from django import forms
class HomeForm(forms.Form):
post = forms.CharField()
home.html (snippet)
<div class="container">
<div class="jumbotron">
<h1>Query Form</h1>
<p>Please Enter the parameters you want to query against the USGS Earthquake DB</p>
<div class="container">
<form class="" method="post" action="">
{% csrf_token %}
{{ myform }}
<button type="submit" class="btn btn-success">Search</button>
</form>
</div>
</div>
</div>
Django Project urls (interview.py/urls.py)
from django.contrib import admin
from django.urls import path, include
from interview.views import login_redirect
from interview import views
from django.contrib.auth.views import LoginView
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('', login_redirect, name='login_redirect'),
path('admin/', admin.site.urls),
path('home/', include('earthquake.urls')),
path('login/', LoginView.as_view(template_name='earthquake/login.html'), name="login"),
path('logout/', LogoutView.as_view(template_name='earthquake/logout.html'), name="logout"),
path('register/', views.register, name='register'),
]
App URLS (interview/earthquake/urls.py)
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Folder Structure
https://i.stack.imgur.com/zoehT.jpg
(In case you're unable to see the last entry in the image, it's the main views.py present in the project folder).
The following is the snapshot of the render I currently get:
https://i.stack.imgur.com/kXR7W.jpg
I see that in your home views file your class based view is called
HomeView(TemplateView)
Yet in your app urls you are including the view as view.home when it should be
view.HomeView
to add to that, this is a classed based view so your urls page for that should look like:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home.as_view(), name='home'),
]
Since this is a class based view.
So I have been trying to access a project post that I have created for my website, but every time I click on it the url can't be found. I am not sure why.
My code is below:
mywebsite/urls.py
from django.contrib import admin
from django.urls import re_path, include
from django.conf import settings
from django.conf.urls.static import static
#re_path(r'^admin/', admin.site.urls),
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('home.urls')),
re_path(r'^projects/', include('projects.urls', namespace="create_post")),
re_path(r'^contact/', include('contact.urls')),
]
projects/urls.py
from django.urls import re_path, include
from . import views
# urls for projects page
app_name = 'create_post'
urlpatterns = [
re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
#re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]
# To make images work
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects/views.py
# Function to retrieve all different projects
def retrieve_projects(request):
# Retrieves objects based on latest publish date
projects_list = Projects.objects.all().order_by("-publish_date")
context = {
'projects_list': projects_list,
}
return render(request, 'projects/projects.html', context)
projects/projects.html
<h1>asdasdasdasdasdasdas</h1>
{% if projects_list %}
<ul>
{% for project in projects_list %}
<h2>{{project.title}}</h2>
<h2>{{project.description}}</h2>
<h2>{{ project.publish_date }}</h2>
{% endfor %}
</ul>
{% endif %}
The error returns page not found
try this
I am getting the error when resolving url http://127.0.0.1:8000/userprofile/auth. Below is my urls.py file for the userprofile app:
from django.conf.urls import patterns, url
from userprofile import views
urlpatterns = patterns('',
url(r'^$', views.login),
url(r'^auth/$' , views.auth_view),
url(r'^logout/$', views.logout),
url(r'/loggedin/$', views.loggedin),
url(r'/invalid/$', views.invalid_login),
)
The main urls.py is as follows:
from django.conf.urls import patterns, include, url
from userprofile import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^userprofile/', include('userprofile.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Here's a link to the screenshot of browser.
You should add the / slash to the action attribute of your <form>:
<form action="/userprofile/auth/" method="POST">
Or, as the better solution, name the url:
url(r'^auth/$' , views.auth_view, name='auth_view'),
and use the {% url %} tag in the template:
<form action="{% url 'auth_view' %}" method="POST">
I managed to create a URL tag for my index. But right now I'm confused how to add links to other pages.
I put this on my urls.py
url(r'^$', 'index', name='index'),
The next thing I put this tag into the href:
{% url 'index' %}
But what if I wanted to create a new page and would be linking to it. How would I do it the best way?
So next you would extend your urls.py to look something like this:
url(r'^$', 'index', name='index'),
url(r'^blog$', 'blog', name='blog'),
Then in your html you can use either one:
Home
Blog
You can of course use the template tage {% url 'index' %} as many times as you need in any template.
Django has updated urlpatterns to take 'path' instead of using url so it's become much more efficient. You don't have to use regex anymore
from django.urls import path
from . import views
urlpatterns=[
path('', views.index , name='index'),
path('blog/', views.blog , name='blog'),]
Then in templates, you can use template tagging
Index
Blog
If you have multiple apps, you can tag it as follows. For example, if this is under 'post' app:
In post app urls.py:
from django.urls import path
from . import views
app_name = 'post'
urlpatterns=[
path('', views.index , name='index'),
path('blog/', views.blog , name='blog'),]
in the project urls.py:
from django.urls import path, include
urlpatterns=[
path('post/', include('post.urls'),]
In templates, you do as following:
Index
Blog
Just use the same label {% url 'index' %}.
You may use each name in urls.py to link to the url.
urls.py
url(r'^archive/$', 'mysite.views.archive',name='archive'),
url(r'^about/$', 'mysite.views.about',name='about'),
url(r'^contact/$', 'mysite.views.contact',name='contact'),
template
About
Contact
If you have many apps, use namespace
https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces-and-included-urlconfs
Use the below syntax:
<a href="{% url 'cart' %}" > Cart page </a>
Where URL 'cart' is the name field in your app URL pattern.
urlpatterns = [
#Leave as empty string for base url
path('', views.shop, name="store"),
path('cart/', views.cart, name="cart"),--------------------->
path('checkout/', views.checkout, name="checkout"),
path('shop/',views.shop, name="shop"),
]
Create a new URL in the same format and give that name instead of index.
Eg:
url(r'^$', 'index', name='index'),
url(r'^new/page/$', 'new', name='new_page'),
{% url 'new_page' %}
Example:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about', name='about'),
)
Now, in the html template rendered by your views.index you can have:
about