I have some error with Rest API when started Django project.
The error is
"raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'api.urls' from '/Users/luba/code/library/library_project/api/urls.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."
My code:
api/views.py
from rest_framework import generics
from books.models import Book
from .serializers import BookSerializer
class BookAPIView(generics.ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
api/urls.py
from django.urls import path
from .views import BookAPIView
urlpattens = [
path('', BookAPIView.as_view()),
]
api/serializers.py
from rest_framework import serializers
from books.models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('title', 'subtitle', 'author', 'isbn')
books/views.py
from django.urls import path
from .views import BookListView
urlpatterns = [
path('', BookListView.as_view(), name='home'),
]
books/views.py
from django.views.generic import ListView
from .models import Book
class BookListView(ListView):
model = Book
template_name = 'book_list.html'
Somebody can explain, in which staff problem with my code.Will be appreciate!!!!
It seems like you didn't include your api urls to main urls.py
You can find it at same folder where your settings.py is located.
from django.urls import path, include
from api import urls
urlpatterns = [
path('api/', include(urls.urlpatterns)),
]
Related
I have a Datatable that's working perfectly, and I wanted Server-side process to be true and not client-side.
But when I enable server-side the sorting, paging and search became faulty.
TestCase: https://codepen.io/TheNerdy97/pen/gOvNeeo?html-preprocessor=pug
I'm using this module https://github.com/izimobil/django-rest-framework-datatables
For seamless integration between Django REST framework and Datatables.
I did as the doc said but it still won't work as expected.
models.py
from unittest.util import _MAX_LENGTH
from django.db import models
#from django.utils.translation import gettext as __
# Create your models here.
class Fresh(models.Model):
code = models.CharField(("code"), max_length=255)
orbs = models.IntegerField(("orbs"))
orbs_display = models.CharField(('orbs_display'), max_length=255)
price = models.IntegerField(("price"))
price_display = models.CharField(('price_display'), max_length=255)
images = models.CharField(("images"), max_length=10000000)
date = models.CharField(("date"), max_length=255)
class Meta:
ordering = ['code', 'orbs', 'price']
def __str__(self):
return self.code
views.py
from django.shortcuts import render
from accounts.models import Fresh, Orbs
from rest_framework import viewsets
from accounts.serializers import FreshSerializer
from django.core import serializers
# Create your views here.
def index (request):
return render(request, 'index.html')
class FreshViewSet(viewsets.ModelViewSet):
queryset = Fresh.objects.all().order_by('price')
serializer_class = FreshSerializer
urls.py
import accounts
from . import views
from django.urls import path, include, re_path
from rest_framework import routers
from main import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'fresh', views.FreshViewSet)
router.register(r'orbs', views.OrbsViewSet)
urlpatterns = [
path('', views.index, name='index'),
path('fresh', views.fresh, name='fresh'),
re_path('^api/', include(router.urls)),
]
When I open the server(Django) I get this error: "The included URLconf 'admin.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."
I have found out that by removing .views import from urls.py I fix the issue. So I think the problem is inside views.py.
App urls.py
from django.contrib import admin
from django.urls import include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('/api', include('crud.urls')),
]
views.py
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import User
from .serializers import UserSerializer
class UserView(APIView):
def get(self, request):
users = User.objects.all()
serializer = UserSerializer(users, many=True)
return Response({"users": users})
serializer.py
from rest_framework import serializers
class UserSerializer(serializers.Serializer):
name = serializers.CharField(max_length=255)
email = serializers.EmailField()
password = serializers.CharField(max_length=255)
disease = serializers.CharField(max_length=255)
logo = serializers.TextField()
crud urls.py
Here, the issue is at the second line: if I remove that line I fix the error
from django.urls import path
from .views import UserView
app_name='crud'
# app_name will help us do a reverse look-up latter.
urlpatterns = [
path('users/', UserView.as_view()),
]
Please add error trace for better understanding of the issue.
Meanwhile try changing this:
path('/api', include('crud.urls'))
to:
path('api/', include('crud.urls')),
and
from .views import UserView
to:
from crud import views
urlpatterns = [
path('users/', views.UserView.as_view()),
]
http://127.0.0.1:8000/contextual/main/ works, but for some reason http://127.0.0.1:8000/contextual/main/create/ says the url does not exist, even if I included it. What's wrong?
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django.urls import path, re_path, include
urlpatterns = [
#url('admin/', admin.site.urls),
#url(r'^user/', include('base.urls')),
#url(r'^contextual/', include('base.urls')),
#url(r'^home/', TemplateView.as_view(template_name='home.html'), name='home'),
#url(r'^plots/', include('plots.urls')),
# url(r'^welldata/', include('welldata.urls')),
# eric's
path('contextual/', include('eric_base.urls'))
eric_base/urls.py
from django.urls import re_path, include
from eric_base import views as base_views
app_name = 'eric_base'
urlpatterns = [
re_path(r'^main/$', include([
re_path(r'^$', base_views.ContextualMainView.as_view(), name='main'),
re_path(r'^create/$', base_views.WellCreateView.as_view(), name='create'),
])),
]
views.py
from django.shortcuts import render
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView
from . import models
class ContextualMainView(TemplateView):
template_name = 'contextual_main.html'
class WellCreateView(CreateView):
template_name = 'practice_add_well.html'
model = models.WellInfo
fields = '__all__'
models.py
from django.db import models
# Create your models here.
class WellInfo(models.Model):
name = models.CharField(max_length=100)
region_location = models.CharField(max_length=100)
spud_date = models.CharField(max_length=100)
well_bore = models.CharField(max_length=100)
rig_name = models.CharField(max_length=100)
status = models.CharField(max_length=100)
You have a terminating $ after the main pattern, so no further characters will be matched. You should remove that.
re_path(r'^main/', include([...]))
I am working through this tutorial on rapid site development with Django.
I have followed it exactly (as far as I can see), but get the following error when I try to view the index page:
NameError at /name 'views' is not defined
Exception location: \tuts\urls.py in <module>, line 12
Here's urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
)
Here's views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
items = Item.objects.order_by("-publish_date")
now = datetime.datetime.now()
return render(request,'portfolio/index.html', {"items": items, "year": now.year})
And here's models.py:
from django.db import models
# Create your models here.
class Item(models.Model):
publish_date = models.DateField(max_length=200)
name = models.CharField(max_length=200)
detail = models.CharField(max_length=1000)
url = models.URLField()
thumbnail = models.CharField(max_length=200)
I also have a basic index.html template in place. From looking around I think I need to import my view somewhere.
But I'm completely new to Django so I have no clue. Any ideas?
The error line is
url(r'^$', views.index, name='index'),
#----------^
Here views is not defined, hence the error. You need to import it from your app.
in urls.py add line
from <your_app> import views
# replace <your_app> with your application name.
from .views import index
here we have to import views model classes on urls model so above sentence import your view model class on urls model.
add this code in urls.py
If you are using rest_framework in django then import:
from rest_framework import views
I use django rest framework and have this error:
base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .model or .queryset attribute.
This is my code
urls.py
from django.conf.urls import patterns, include, url
from rest_framework import viewsets, routers
import views
router = routers.SimpleRouter()
router.register(r'book', views.BookViewSet.as_view())
views.py
from django.shortcuts import render_to_response
from mobileapp.models import Book
from rest_framework import generics
from mobileapp.serializers import BookSerializer
class BookViewSet(generics.ListAPIView):
serializer_class = BookSerializer
def get_queryset(self):
queryset = Book.objects.filter(user=self.request.user)
return queryset.order_by('-id')
serializers.py
from mobileapp.models import Book
from rest_framework import serializers
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('id', 'url', 'date', 'comment')
Have you google'd error statement?
https://github.com/tomchristie/django-rest-framework/issues/933
http://django-rest-framework.org/api-guide/routers.html#usage
I'v solved my problem. There is code.
urls.py
from django.conf.urls import patterns, include, url
from views import BookList, BookDetail
from rest_framework.urlpatterns import format_suffix_patterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^book/$', BookList.as_view(), name='book-list'),
url(r'^book/(?P<pk>\d+)/$', BookDetail.as_view(), name='book-detail'),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'api'])
views.py
class BookList(generics.ListCreateAPIView):
serializer_class = BookSerializer
def get_queryset(self):
queryset = Book.objects.filter(user=self.request.user)
return queryset.order_by('-id')
class BookDetail(generics.RetrieveUpdateDestroyAPIView):
model = Book
serializer_class = BookSerializer