django - 'function' object has no attribute 'resolve' - python

I am trying to learn about class based views and django in general. The project is notes_project and in it I have created an app notes. Below is the urls.py for both of these and views.py for notes app:
notes_project/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
import notes
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'notes_project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^notes/', include('notes.urls')),
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls)),
)
notes/urls.py
from django.conf.urls import include, patterns, url
from .views import IndexView
urlpatterns = patterns(r'^$/', IndexView.as_view())
notes/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View
class IndexView(View):
def get(request):
return HttpResponse("Welcome to notes index")
However, whenever I access the URL http://127.0.0.1:8000/notes/, I keep getting below error:
Request Method: GET
Request URL: http://127.0.0.1:8000/notes/
Django Version: 1.7.4
Exception Type: AttributeError
Exception Value:
'function' object has no attribute 'resolve'
Exception Location: /path/notes/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in resolve, line 345
Python Executable: /path/notes/venv/bin/python
Python Version: 3.4.2
Python Path:
['/path/notes/notes_project',
'/path/notes/venv/lib/python3.4',
'/path/notes/venv/lib/python3.4/plat-x86_64-linux-gnu',
'/path/notes/venv/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/path/notes/venv/lib/python3.4/site-packages']

The first argument to patterns is a string that acts as prefix to the rest of the patterns. Also each pattern needs to be a tuple of its own. You've done that correctly in the main urls.py but missed it in the notes one. It should be:
urlpatterns = patterns('',
(r'^$', IndexView.as_view()),
)

Related

django 4.1 polls tutorial, all done Admin working fine but index not found

ok polls tutorial is done and can enter questions, vote, etc. but from the admin pages View site aka localhost:8000 gives...
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
dose/
admin/
The empty path didn’t match any of these.
Now I have polls/views.py...
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils import timezone
from django.views import generic
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
return Question.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
and mysite/urls.py set to ...
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
and polls/urls.py set to...
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
The only hint I can find is that all the imports show error highlights in VSCode with the Error "reportMissingModuleSources"
But python and django run perfectly well everywhere but index.html
No url at http://localhost:8000/ address is expected behaviour, where you want to go is http://localhost:8000/polls/.
If you want go directly to the desired View without writing polls/ prefix you should define your urls this way:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
So you place path() call without url prefix. Generally if you include any url group you define the beginning of address this way.

I'm following the Django documentation 01 but it can't work for me

I'm a beginner at Django.
views.py/my_app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
urls.py/my_app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
urls.py/my_project
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('product/', include('product.urls')),
path('admin/', admin.site.urls),
]
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/product
Using the URLconf defined in Zero.urls, Django tried these URL patterns, in this order:
admin/
The current path, product, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

django attribute error ------>AttributeError: module 'django.contrib.auth.views' has no attribute 'login', 'logout'

AttributeError: module 'django.contrib.auth.views' has no attribute 'login', 'logout'
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from blog import views
from django.contrib.auth import views
urlpatterns = [
path('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':'/'}),
]
The function based views are deprecated, you need to use the class-based alternatives
url(r'accounts/login/$',views.LoginView.as_view(), name='login'),
url(r'accounts/logout/$',views.LogoutView.as_view(next_page='/'), name='logout'),
You're also importing views twice the first import from blog is being overridden by the import from auth

<module 'myblog.urls' from 'path\\to\\python\\file\\urls.py'>' is not a callable or a dot-notation path

I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.

Django Tutorial: Generic Views. Attribute Error

I'm at the last part of this tutorial.
from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll
urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html')),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/detail.html')),
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model=Poll,
template_name='polls/results.html'),
name='poll_results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)
The ListView works, but when I visit a url with DetailView, I get.
AttributeError at /polls/2/
Generic detail view DetailView must be called with either an object pk or a slug.
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/2/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
Generic detail view DetailView must be called with either an object pk or a slug.
Exception Location: /home/yasith/coding/django/django-tutorial/lib/python2.7/site-packages/django/views/generic/detail.py in get_object, line 46
Python Executable: /home/yasith/coding/django/django-tutorial/bin/python2
Python Version: 2.7.3
I'm not sure what I'm doing wrong. Any help would be appreciated.
EDIT: Add the main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think the code you posted above, is not the one you have on your disk.
I had the same problem, but then I looked carefully at both, my code and the tutorial. The regex I had in my code was different from the tutorial.
This was my code:
url(r'^(?P<poll_id>\d+)/$',-$
url(r'^(?P<poll_id>\d+)/results/$',-$
This is the correct core:
url(r'^(?P<pk>\d+)/$',-$
url(r'^(?P<pk>\d+)/results/$',-$
Note that *poll_id* was in the previous sections of the tutorial, but generic views require pk. Also note that the tutorial is correct, and you posted the correct code (from the tutorial.)
Look Carefully in the Tutorials they have mentioned to change the urlpatterns to use primarykey instead of question_id.

Categories