django urls error when I import a non-itinerant object - django

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

Related

Django DRF APITestCase post url results in 404 error

I'm learning DRF test cases, and in my test.py file, my URL in the client post-call is coming back in a 400 status error:
Here's my urls.py:
from django.contrib import admin
from django.urls import path, include
#from rest_auth.views import LoginView, LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path("api/", include("profiles.api.urls")),
path("api-auth/", include("rest_framework.urls")),
path("api/rest-auth/", include("rest_auth.urls")),
path("api/rest-auth/registration/", include("rest_auth.registration.urls"))
]
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here's my test.py file
import json
from django.contrib.auth.models import User
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from rest_framework import status
from profiles.models import Profile
from profiles.api.serializers import ProfileSerializer
class RegistrationTestCase(APITestCase):
def test_registration(self):
data = {"username": "testuser1", "email": "test#localhost.app", "password1": "A41&14all", "password2": "A41#14all"}
response = self.client.post("/api/rest-auth/registration/", data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
It appears that the line self.client.post.... fails to find the endpoint.
What am I missing?
thanks!

Importing from views directory

In my project I have an app called api where I keep my viewsets in a directory called views. I keep getting ModuleNotFoundError: No module named 'api.views.book'; 'api.views' is not a package where I import in my api/urls.py
api/urls.py
from rest_framework import routers
from api.views.book import BookViewset
router = routers.DefaultRouter()
router.register(r'books', BookViewset, 'books')
api_urls = router.urls
In my main urls.py I'm doing this:
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from api.urls import api_urls
urlpatterns = [
path('api', include(api_urls)),
path('admin/', admin.site.urls),
]
I didn't get an error until I imported api_urls
I should mention that api is included in INSTALLED_APPS
You need to include a file named __init__.py in a directory to make it a package, then you can import from it
In your case you need a file api/views/__init__.py

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')
'''

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