I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django, it is giving me this error message:
django.core.exceptions.ImproperlyConfigured: The included URLconf 'starsocial.urls' 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.
The issue started when I added a new app which has a urls.py like the following:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r'login/$',
auth_views.LoginView.as_view(template_name='accounts/login.html'),
name='login'),
url(r'logout/$',auth_views.LogoutView.as_view(), name='logout'),
url(r'signup/$',views.SignUp.as_view(), name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code:
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^$', views.HomePage.as_view(), name='home'),
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^test/$', views.TestPage.as_view(), name='test'),
url(r'^thanks/$', views.ThanksPage.as_view(), name='thanks')
]
My application's view looks like the following:
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
My project's view looks like:
from django.views.generic import TemplateView
class TestPage(TemplateView):
template_name = 'test.html'
class ThanksPage(TemplateView):
template_name = 'thanks.html'
class HomePage(TemplateView):
template_name = 'index.html'
Can anyone please help me identify where I could possibly be going wrong.
You are importing auth.urls twice. Remove url(r'^accounts/', include('django.contrib.auth.urls')) from your project's urls.py
I am importing wrong URL configuration, instead of 'reverse' I should import 'reverse_lazy',
change
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
to
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy('login')
template_name = 'accounts/signup.html'
Related
where can I import the detail attribute from?
views.py:
from django.shortcuts import render
from django.views import generic
from . import models
class Index(generic.TemplateView):
template_name='catalog/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['num_books'] = models.Book.objects.all().count()
context['num_instances'] = models.BookInstance.objects.all().count()
context['num_instances_available'] = models.BookInstance.objects.filter(status__exact='a').count()
context['num_authors'] = models.Author.objects.count()
return context
class BookListView(generic.ListView):
model = models.Book
template_name = 'catalog/book_list.html'
class BookDetialView(generic.Detail.View):
model= models.Book
template_name = 'catalog/book_detail.html'
urls.py
from django.urls import include, path, re_path
from . import views
from django.views.generic import TemplateView
app_name='catalog'
urlpatterns = [
path(r'', views.Index.as_view(), name='index'),
re_path(r'^$', views.Index.as_view(), name='index')
]
urlpatterns = [
path(r'^$', views.index, name='index'),
path(r'^books/$', views.BookListView.as_view(), name='books'),
path(r'^book/(?P<pk>\d+)$', views.BookDetailView.as_view(), name='book-detail'),
]
but the result:
class BookDetialView(generic.Detail.View):
AttributeError: module 'django.views.generic' has no attribute 'Detail'
Should be generic.DetailViewinstead of generic.Detail.View
In your BookDetailView you used generic.Detail.View instead of DetailView[Djano-doc].
Change your BookDetailView as
from django.views import generic
class BookDetialView(generic.DetailView): #<--- change here
model= models.Book
template_name = 'catalog/book_detail.html'
You have a couple of issues here.
(1) In your views you spell your view name as BookDetialView and in your url definition you spell it BookDetailView.
(2) I believe the correct inheritance is generic.DetailView instead of generic.Detail.View
Under my once my user has signed up I would like to redirect them to the login page. However, reverse_lazy doesn't seem to be able to find my login path.
views.py
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUpView(CreateView):
form_class = forms.UserCreateForm
template_name = 'accounts/signup.html'
#Redirect login page once they have successfully signed up
succes_url = reverse_lazy('login')
urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
path('signup/', views.SignUpView.as_view(template_name='accounts/signup.html'), name='signup'),
]
That is because you specified an app_name = …, in that case you prefix the name of the view with the namespace:
class SignUpView(CreateView):
# …
success_url = reverse_lazy('accounts:login')
You furthermore made a typo, it is success_url [Django-doc], not succes_url.
I am getting a TypeError after adding TemplateView to my views folder.
I've done a test project earlier and it worked pretty fine.I am a beginner in django
#views.py
```python
from django.shortcuts import render
from django.views.generic import TemplateView
class HomeResponse(TemplateView):
template_name = 'home.html'
#urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomeResponse, name='blog_name'),
]
```
[1]: https://i.stack.imgur.com/5XpRZ.png
urlpatterns = [
path('', views.HomeResponse.as_view(), name='blog_name'),
]
This shall work, you should access CBV's by calling as_view() method on them.
Sorry, I am still new at django. I want to make custom view at admin site that is not related to my model. I have read the documentation (https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls), but does not work. Reading some tutorials does not work too...
Here is what I tried:
admin.py
from django.contrib import admin
from django.urls import path
from .models import Question
from django.http import HttpResponse
class CustomAdminView(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'^my_view/$', self.admin_site.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
admin.site.register(Question)
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
admin.autodiscover()
urlpatterns = [
path(r'polls/',include('polls.urls')),
path('admin/', admin.site.urls),
]
when I go to admin/my_view the result is 404 not found.
I tried by extending the AdminView too.
admin.py
from django.contrib.admin import AdminSite
from django.urls import path
from .models import Question
from django.http import HttpResponse
class CustomAdminView(AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'my_view/', self.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
custom_admin = CustomAdminView()
custom_admin.register(Question)
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from polls.admin import custom_admin
admin.autodiscover()
urlpatterns = [
path(r'polls/',include('polls.urls')),
path('admin/', custom_admin.urls),
]
I don't get 404 error on admin/my_view. But, the default models(user, and others) are not displayed. There is only my 'Question' model there. The previous one still has the default models.
How can I make the custom admin view with the right way?
Thanks.
It is solved. I am using my second admin.py and urls.py snippets and register django's default model, based on this answer: Django (1.10) override AdminSite
admin.py
from django.contrib.admin import AdminSite
from django.http import HttpResponse
from django.urls import path
from .models import Question
from django.contrib.auth.models import Group, User #add these moduls
from django.contrib.auth.admin import GroupAdmin, UserAdmin #and these
class CustomAdminView(AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'my_view/', self.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
custom_admin = CustomAdminView()
custom_admin.register(Question)
#register the default model
custom_admin.register(Group, GroupAdmin)
custom_admin.register(User, UserAdmin)
Django fires exception cannot import name TemplateView how to fix this?
view.py :
from django.views.generic import TemplateView
class Monitor(TemplateView):
template_name = 'helo.html'
urls.py :
from monitor.views import Monitor
urlpatterns = patterns('',
(r'^admin/', Monitor.as_view()),
)
I don't know what Django version you are using, but only in Django 1.3 a class called TemplateView exists. Its import should be:
from django.views.generic.base import TemplateView