Not getting the required interface everytime i search using/admin , - python

I am following a yt tutorial on django(https://www.youtube.com/watch?v=sm1mokevMWk)
where the tutor searches http://127.0.0.1:8000/admin and gets the required interface
(It starts around 49:00)
everytime i search http://127.0.0.1:8000/admin
i get a
DoesNotExist at /admin
but when i search http://127.0.0.1:8000/admin/login/?next=/admin/nw/
i get the required interface
A question exactly same as mine has been asked before but the thing is i did not make the same error as that person did
this is my views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList,Items
def index(response,name):
ls=ToDoList.objects.get(name=name)
item=ls.items_set.get(id=1)
return HttpResponse("<h1>%s</h1><br></br><p>%s</p>"%(ls.name,str(item.text)))
# Create your views here.
this is my urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.url")),
]
this is my models.py
from django.db import models
class ToDoList(models.Model):
name=models.CharField(max_length=200)
def __str__(self):
return self.name
class Items(models.Model):
todolist=models.ForeignKey(ToDoList,on_delete=models.CASCADE)
text=models.CharField(max_length=300)
complete=models.BooleanField()
def __str__(self):
return self.text

I shouldnt have added the '/ ' in /admin which is inside the urls.py file

Related

Python Why Django won't let me load a new product

I'm on this page: Filling the details
The link to the orange image is https://upload.wikimedia.org/wikipedia/commons/c/cb/Oranges_white_background.jpg
When I'm pressing 'save' it gives me this: Error message
Couldn't understand the issues I tried various ways to solve them like take another picture and re-run the server. From Mosh Hamedi tutorial: https://www.youtube.com/watch?v=_uQrJ0TkZlc&t=20225s at 05:42:20
I do not know what this double space between 'user' and 'old' means and I really need help with this, I'm stuck on this from Sunday:
models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.FloatField()
stock = models.IntegerField()
image_url = models.CharField(max_length=2083)
class Offer(models.Model):
code = models.CharField(max_length=10)
description = models.CharField(max_length=255)
discount = models.FloatField()
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
def new(request):
return HttpResponse('New Products')
admin.py
from django.contrib import admin
from .models import Product
admin.site.register(Product)
pyshop - urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("products/", include('products.urls'))
]
products - urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('new', views.new)
]
Hey dont use Charfield with specific length in image field. Use either UrlField or an ImageField in models.
Then try to add from admin panel.
And also run makemigrations and migrate command.

Python Why Django won't let me load a new product?

I'm in this page:
Filling the details
The ling is https://upload.wikimedia.org/wikipedia/commons/c/cb/Oranges_white_background.jpg
When I'm pressing 'save' it gives me this: Error message
Couldn't understand the issues I tried various ways to solve it like take another picture and re-run the server.
From Mosh Hamedi tutorial: https://www.youtube.com/watch?v=_uQrJ0TkZlc&t=20225s at 05:42:20
edit: adding code.
models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.FloatField()
stock = models.IntegerField()
image_url = models.CharField(max_length=2083)
class Offer(models.Model):
code = models.CharField(max_length=10)
description = models.CharField(max_length=255)
discount = models.FloatField()
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
def new(request):
return HttpResponse('New Products')
admin.py
from django.contrib import admin
from .models import Product
admin.site.register(Product)
pyshop - urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("products/", include('products.urls'))
]
products - urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('new', views.new)
]
Django is not able to find the table
main.auth_user__old
observe the double underscore between user and old. Are you sure it is correct?
Also, please upload your code.

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

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

OperationalError at /admin/todo/todo/ in Django

I'm making a basic Todo app in Django.
While going to the admin page and clicking on the Todo option:
It gives me this error:
The "todo" string appears twice in the URL.
I have already done the migrations thing and I have added the todo.apps.TodoConfig in the INSTALLED_APPS.
Here is my code:
todo app urls.py
from django.urls import path
from todo import views
urlpatterns = [
path('', views.index),
path('todo/', views.index,)
]`
todo app views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello ")
todo app models.py
from django.db import models
from datetime import datetime
class Todo(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title
todo app admin.py
from django.contrib import admin
from .models import Todo
admin.site.register(Todo)
The main project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('ToDoList/', include('ToDoList.urls')),
path('Todo/', include('todo.urls')),
]
try saving the admin.py file
maybe in admin.py add this code:
from django.contrib import admin
from .models import Todo
admin.site.register(Todo)
Check It And Go!!!

Categories

Resources