django - The current path ... didn't match any of these - python

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([...]))

Related

Datatables| Django, sorting, paging and searching with DRF

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)),
]

Django error 404 2458 when requesting objects by id

I am following this video trying to learn Django. I have completed this tutorial and I had the same problem even when I followed the code to a T.
I am trying to get information about a model object displayed on the web-page when entering the id of the object directly in the url like http://localhost:8000/app/item/1/ or http://localhost:8000/app/item/2/ as the video shows (7:30 into the video). But when I try, I get this error:
Original code from video:
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book
def index(request):
return HttpResponse('Hello, world!')
def book_by_id(request, book_id):
book = Book.objects.get(pk=book_id)
return HttpResponse(f"Book: {book.title}, published on {book.pub_date}")
models.py:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
urls.py:
from django.utils import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('book/<int:book_id>', views.book_by_id, name='book_by_id'),
]
My code:
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Item
def index(request):
return HttpResponse('Hello, world!')
def item_by_id(request, item_id):
item = Item.objects.get(pk=item_id)
return HttpResponse(f"Item: {item.title}, published on {item.datetime_found}")
models.py:
from django.db import models
from django.utils.timezone import now
class Item(models.Model):
title = models.CharField(max_length=200) # Short description of the item
datetime_found = models.DateTimeField(default=now, blank=True) # Date and time of when the item was found
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('item/<int:item_id>', views.item_by_id, name='item_by_id'),
]
Project-level urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('myapp.urls')),
path('admin/', admin.site.urls),
]
What am I not getting right about the GET-request? I feel like the changes I've made are minimal. And I have migrated everything correctly. (I think)
Use this
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
or you can remove 'app' from your url and make it look like this http://127.0.0.1:8000/item/1
look at url pattern of yours and the original.
path('item/<int:item_id>', views.item_by_id, name='item_by_id'),
is yours
path('book/<int:book_id>', views.book_by_id, name='book_by_id'),
is the original

Rest API . Help to change ListView

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)),
]

Circular import error when I open the Django server

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()),
]

error with serialization in django rest framework

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

Categories

Resources