ImportError: cannot import name views while running the server - django

I am getting following error and donĀ“t know how to solve this:
from . import views
ImportError: cannot import name views
This are my scripts i am using:
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$',views.home,name='index'),
url(r'^admin/', admin.site.urls),]
views.py
from __future__ import unicode_literals
from django.http import HttpResponse
def index(request):
return HttpResponse("libaray management system")

Try to import like this
from your_app_name import views
I can't say, what is your project structure and where are your views.py and urls.py files. My guess, that you have some problems with how you import (check the absolute and relative import in python). In that case you can use import style from above code example.

Related

django urls error when I import a non-itinerant object

I can't understand why it gives me an error when importing my views.
I don't understand where the mistake is. I deleted some apps from the project and views, will it be for that?
views
from django.shortcuts import render
from django.views.generic import ListView
from .views import Schede
class SchedeListView(ListView):
model = Schede
template_name = 'test.html'
context_object_name = 'schede'
devtest urls
from django.urls import path
from django.views.generic import TemplateView
from .views import views
urlpatterns = [
path('', TemplateView.as_view(template_name="dash.html"), name="home"),
path('test_schede/', views.SchedeListView.as_view(), name="test_schede"),
]
url general
from django.contrib import admin
from django.urls import path, include
#sitemap
from django.contrib.sitemaps.views import sitemap
from .sitemaps import StaticViewSitemap
#pdf
from pdf.views import testPdfView
sitemaps = {'static': StaticViewSitemap}
urlpatterns = [
path('admin/', admin.site.urls),
path('test/<id>/', testPdfView, name="test_pdf"),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}),
path('', include('devtest.urls'))
]
error
C:\Users\dev\Downloads\laragon\www\scheda>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\dev\Downloads\laragon\bin\python\python-3.6.1\lib\site-packages
\django\urls\resolvers.py", line 600, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
Are your imports fine? If Schede is a model, it should be imported from models.py I believe. And it's better to specify your_app name in imports.
from .views import Schede
maybe it should be:
from your_app.models import Schede
Another thing, are you sure this import is fine?
from .views import views
Maybe it should be like this:
from your_app import views

Not able to map a view with url

I am facing some problem while mapping a view with URL
Project name-Hero
app name-Movie
Hero/url.py:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'^',include('Movie.urls'))
]
Movie/urls.py:
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='Movie'),
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello friends ,Welcome to Django Project</h1>")
These are my urls and views configurations.I am getting a 404 page.
Please help me out.. Thanks
Try with below code, only changes in Hero/url.py
Hero/url.py
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'',include('Movie.urls')) # Do not add caret sign if further urls contains caret.
]

django rest framework "ModuleNotFoundError: No module named 'router'"

i have imported all the restframework packages but i don't understand why am i getting this strange error
from django.conf.urls import *
from django.contrib import admin
from django.views.generic.base import TemplateView
from .views import Create,Home,signup,Search,QuestionViewSet
from django.urls import reverse
from .models import Question
from django.contrib.auth import views as auth_views
from rest_framework import routers
router=routers.DefaultRouter()
router.register(prefix='question1',viewset=QuestionViewSet)
app_name='main'
urlpatterns = [
# url(r'^/',views.home,name='home'),
url(r'^home/',Home,name='home'),
url(r'^ques/',Create.as_view(success_url="/index/home/"),name='ques'),
url(r'^signup/',signup,name='signup'),
url(r'^logout/$', auth_views.logout,name='logout'),
url(r'^search/',Search,name='search'),
url(r'^api/', include('router.urls'))
# CreateView.as_view(model=myModel, success_url=reverse('success-url'))
]
this is the issue i'm facing
ModuleNotFoundError: No module named 'router'
any kind of help is appreciated
thanks in advance
First argument of include method can be module name or pattern list. Since router.urls is patterns list it should be
url(r'^api/', include(router.urls))
instead of
url(r'^api/', include('router.urls'))
So you need to remove '' signs.

ImportError: cannot import name Upload

I am working on Django, django-rest project and as I have searched the problem, it is beleived I have circular problem, but I don`t think so...
Relevant parts of problem are here:
demo/views.py
from django.shortcuts import render
from rest_auth.models import Upload, UploadForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
def home(request):
if request.method=="POST":
img = UploadForm(request.POST, request.FILES)
if img.is_valid():
img.save()
return HttpResponseRedirect(reverse('imageupload'))
else:
img=UploadForm()
images=Upload.objects.all()
return render(request,'home.html',{'form':img,'images':images})
rest_auth/models.py
from django.conf import settings
from rest_framework.authtoken.models import Token as DefaultTokenModel
from .utils import import_callable
from django.db import models
from django.forms import ModelForm
class Upload(models.Model):
pic = models.ImageField("Image", upload_to="images/")
upload_date=models.DateTimeField(auto_now_add =True)
# FileUpload form class.
class UploadForm(ModelForm):
class Meta:
model = Upload
and
demo/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from django.conf import settings
from django.conf.urls.static import static
from views import home
from rest_framework_swagger.views import get_swagger_view
import os
PROJECT_DIR=os.path.dirname(__file__)
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
url(r'^upload/$', views.home, name='imageupload'),
url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
name='signup'),
Problem is as stated this:
File "/Desktop/zadnja/django-rest-auth/demo/demo/urls.py",
line 6, in
from views import home File "/Desktop/zadnja/django-rest-auth/demo/demo/views.py", line
2, in
from rest_auth.models import Upload, UploadForm ImportError: cannot import name Upload
You do not have circular imports since rest_auth/models.py does not import any component from demo/views.py
I'm assuming rest_auth is an app in you project. To import Upload and UploadForm you should use absolute import:
from project_name.rest_auth.models import Upload, UploadForm

TypeError('view must be a callable or a list/tuple in the case of include().')

I used the Django's version is 1.10.4 and was seeing in my file mysite/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/',include(admin.site.urls)),
url(r'^website/$',"website.views.first_page"),
]
and views.py in mysite/mysite/
# -*- coding: utf-8 -*-
from django.http import HttpResponse
def first_page(request):
return HttpResponse("<p>hello Django</p>")
Did these settings but I always have the current error.Help me.
String reference is deprecated in Django 1.10.
So, Django 1.10 no longer allows you to specify views as a string in your URL patterns.
You can no longer pass import paths to url(), you need to pass the actual view function.
The solution is to update your urls.py to include the view callable.
This means that you have to import the view in your urls.py.
Use these urls.py instead :
from django.conf.urls import url,include
from django.contrib import admin
from mysite.views import first_page
urlpatterns = [
url(r'^admin/',include(admin.site.urls)),
url(r'^website/$',first_page)
]