My url not reading my function in views.py in DJANGO (found similar question not able to trace my issue) - django

mysite/urls.py
from django.contrib.auth import views
from django.views.generic.base import TemplateView
urlpatterns = [
url('update_db/(<dbname>)/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
url('mysite/login/', auth_views.LoginView.as_view(),name='login'),
url(r'^fetch/',include('polls.urls')),
url('mysite/logout/$', auth_views.logout, name='logout'),
]
polls.urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
urlpatterns = [
url('update_db/(?P<dbname>\w+)/$', login_required(views.MyView.get), name='x'),
url('', login_required(views.IndexView.as_view())),
]
fetch.html
<td>click</td>
polls/views.py
from django.shortcuts import render
from django.views.generic import TemplateView , ListView
from django.views.generic.edit import UpdateView
# Create your views here.
from django.http import HttpResponse
from .models import Item , DbRestorationLogDetails
class MyView(UpdateView):
logger.error('Something went wrong!')
print "THANKS FOR UPDATING"
template_name='fetch.html'
model = DbRestorationLogDetails
fields =['verification_status']
def get(self,request, dbname):
usname=request.user
print usname
print dbname
if request.method == "GET":
dblog = DbRestorationLogDetails.objects.get(dbname=dbname)
dblog.verification_status = 'Verified'
print dblog.verification_status
dblog.save()
#.update(dblogdetails.verification_status = 'Verified')
return HttpResponseRedirect(request.GET.get('next'))
NOTE: NOT able to reach to MyView.get() and no db update happening. I assume my code is not able to read my function.

Related

Why am I getting error 404 in my django project when everything seems correct?

I have two apps in my Django project:basket and store. In root url files I configured the urls.py like this:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
store/urls.py:
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.all_products, name='all_products'),
path('<slug:slug>/', views.product_detail, name='product_detail'),
path('category/<slug:category_slug>/',
views.category_list, name='category_list')
]
basket/urls.py:
from django.urls import path
from . import views
app_name = 'basket'
urlpatterns = [
path('', views.basket_summary, name='basket_summary'),
path('add/', views.basket_add, name='basket_add'),
]
I am getting an error:
Page not found(404)
Request Method:GET
Request URL:http://127.0.0.1:8000/basket/
Raised by:store.views.product_detail
this is my store/views.py:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
context = {
'product': product,
}
return render(request, 'store/products/detail.html', context)
please help me solve this problem i've been stuck on this for a long time.
Try this way if you want to avoid 404 error:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
try:
product = Product.objects.get(slug=slug, in_stock=True)
context = {
'product': product,
}
except Product.DoesNotExist:
raise ValidationError('Product Does not exist ')
return render(request, 'store/products/detail.html', context
)

does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import .Django 3.0.2

Project name : CRMPROJECT
App name: leads
I'm working on a CRM project With Django and I keep getting this error while trying to map my urls to views
django.core.exceptions.ImproperlyConfigured. The included URLconf does not appear to have any patterns in it
Below is the project/urls.py file
'''
from django.contrib import admin
from django.urls import path,include
from leads import views
urlpatterns = [
path('admin/', admin.site.urls),
path('leads/',include('leads.urls',namespace='leads')),
]
'''
Below is leads/urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('',views.home,name='home'),
]
And lastly my leads/views.py
'''
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'leads/base.html')
'''

AttributeError: module 'calc.views' has no attribute 'home'

I know there are some questions asked already but none of worked for me
server is not running with the command throwing this error
calc urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home, name='home')
]
calc views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello World !")
saish urls.py
from django.contrib import admin
from django.urls import path , include
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]

page not found error in Django using a filter query {return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')}

I am making my first django practice project I have a view called PostListview :
from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from blog.models import Post, Comment
from blog.forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.views.generic import(TemplateView,ListView,DetailView,CreateView,UpdateView,
DeleteView)
class AboutView(TemplateView):
template_name = 'about.html'
class PostListView(ListView):
model = Post
def get_queryset(self):
return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
I have this in in the urls:
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
And this is the template which is calling this view.
<li class="navbar-brand bigbrand" >My Tech Blog</li>
This view is also set as the default home view and it opens fine at the time i open the site url (localhost) but when I am clicking "My Tech blog " it gives a 404 error.
This is the main urls.py :
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
url(r'accounts/login/$',views.login, name = 'login'),
url(r'accounts/logout/$',views.logout, name = 'logout',kwargs={'next_page':'/'}),
]
Just change
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
with
url(r'',views.PostListView.as_view(), name= 'post_list'),

NoReverseMatch at /new_application/check_login/

I am new to DJango. I am getting error 'NoReverseMatch at /new_application/check_login/' while redirecting view from another view.
different files listed below.
Main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^new_application/', include('new_application.urls')),
]
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Login
...
def check_login(request):
...
return HttpResponseRedirect(reverse('new_application:loggedin',args=(user,)))
def loggedin(request, user):
return render(request, 'new_application/loggedin.html',{'full_name': user.first_name +" "+ user.last_name})
Application urls.py
from django.conf.urls import url
from . import views
app_name = 'new_application'
urlpatterns = [
url(r'^$', views.login, name='login'),
url(r'^check_login/$', views.check_login, name='check_login'),
url(r'^loggedin/$', views.loggedin, name='loggedin'),
url(r'^invalid_login/$', views.invalid_login, name='invalid_login'),
url(r'^logout/$', views.logout, name='logout'),
]
Here is an error image : error image
please give the solution for fixing this error.
Thank You.
The regex pattern must match against the given url...
url(r'^loggedin/(?P<user>\w+)/$', views.loggedin, name='loggedin'),
Reverse should match one of URL names from urls.py
return HttpResponseRedirect(reverse('loggedin',args=(user,)))