Django error 404 2458 when requesting objects by id - python

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

Related

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

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

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.

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!!!

Django NameError: name 'views' is not defined

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

Categories

Resources