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!!!
Related
This is the url i am trying:
http://localhost:8000/blog/categoria/1/
The foreign key is categoria_id that comes from relationship many to many of Post and Categoria.
I am using Sqlite3.
This file is the models.py
from django.db import models
from django.contrib.auth.models import User
class Categoria(models.Model):
nombre=models.CharField(max_length=50)
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name='categoria'
verbose_name_plural='categorias'
def __str__(self):
return self.nombre
class Post(models.Model):
titulo=models.CharField(max_length=50)
contenido=models.CharField(max_length=50)
imagen=models.ImageField(upload_to='blog', null=True, blank=True)
autor=models.ForeignKey(User, on_delete=models.CASCADE)
categorias=models.ManyToManyField(Categoria)
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name='post'
verbose_name_plural='posts'
def __str__(self):
return self.titulo
This file is the views.py:
from django.shortcuts import render
from blog.models import Post, Categoria
def blog(request):
posts=Post.objects.all()
return render(request, "blog/blog.html",{"posts":posts})
def categoria(request, categoria_id):
categoria=Categoria.objects.get(id=categoria_id)
posts=Post.objects.filter(categorias=categoria)
return render(request, "blog/categoria.html",{'categoria': categoria, "posts":posts })
This file is the urls.py
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.blog, name='Blog'),
path('categoria/<int:categoria_id>/', views.categoria, name="categoria")
]
I change the database to postgresql and I added a / before categoria in the file urls.py. For now it works. It’s strange, because I’ve made these changes before and it didn't work.
Despite this I would appreciate any suggestion. Thank you.
This file is the urls.py after the change:
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.blog, name='Blog'),
path('/categoria/<int:categoria_id>/', views.categoria, name='categoria'),
]
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.
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
I am trying to add a document file from the admin panel Django but I am getting 404 not found URL errors.
admin file
from django.contrib import admin
from .models import Category, Document
# Register your models here.
admin.site.register(Category)
admin.site.register(Document)
from django.db import models
class Document(models.Model):
doc_name = models.CharField(max_length=100)
doc_slug = models.CharField(max_length=100)
doc_file = models.FileField(upload_to='docs')
doc_price = models.IntegerField()
doc_cat = models.CharField(max_length=100)
doc_desc = models.TextField()
doc_var = models.TextField()
Main URLs
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('UserAccount.urls')),
path('dashboard/', include('Dashboard.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What could possibly be the error?
404 error means that path to the admin panel doesn't declarated.
Check main urls.py file. There should be this import statement: from django.contrib import admin and path('admin/', admin.site.urls) line in urlpatterns var.
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()),
]