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.
Related
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'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!!!
have problems.
1)In production Django tries to access photos at http://"ip_address"/images/"image".jpg URL however images must be in http://"ip_address"/media/images/"image".jpg
2)I have added two more fields (alt and text) to existing model. Migrations work and show that the fields are added but i can not see them in the admin panel ( I have added them to the list_display) and i can not access them in my templates.
settings for media
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'http://"ip_address"/media/'
urls
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
url(r'^$','main.views.mainpage'),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
models
from django.db import models
class Slider(models.Model):
image = models.ImageField(upload_to = 'images/')
caption = models.TextField()
alt = models.CharField(max_length = 20, default = "an image")
text = models.CharField(max_length = 5, null=True)
admin
from django.contrib import admin
from main.models import Slider
class SliderAdmin(admin.ModelAdmin):
list_display = ('image','alt','caption','text')
admin.site.register(Slider,SliderAdmin)
See https://docs.djangoproject.com/en/1.8/howto/static-files/
Add a STATIC_URL to your settings.py
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 am beginner in django and i dont know how to solve this problem
i am getting error on browser as:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order: ^admin/
The current URL, api/Entry/, didn't
match any of these.
all my files are:
mysite/myapp/models.py:
from tastypie.utils.timezone import now
from django.contrib.auth.models import User
from django.db import models
from django.utils.text import slugify
class Entry(models.Model):
user = models.ForeignKey(User)
pub_date = models.DateTimeField(default=now)
title = models.CharField(max_length=200)
slug = models.SlugField()
body = models.TextField()
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
# For automatic slug generation.
if not self.slug:
self.slug = slugify(self.title)[:50]
return super(Entry, self).save(*args, **kwargs)
mysite/myapp/api.py:
from tastypie.resources import ModelResource
from myapp.models import Entry
class EntryResource(ModelResource):
class Meta:
queryset = Entry.objects.all()
resource_name = 'Entry'
mysite/myapp/urls.py:
from django.conf.urls.defaults import *
from myapp.api import EntryResource
entry_resource = EntryResource()
urlpatterns = patterns('',
# The normal jazz here...
(r'^blog/', include('myapp.urls')),
(r'^api/', include(entry_resource.urls)),
mysite/mysite/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))
)
where my 'mysite' is my project name and 'myapp' is my app
please help me out
Thanks
Your project-level urls.py doesn't include your application-level urls.py. Change mysite/mysite/urls.py to this:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls))
)
Also, get rid of (r'^blog/', include('myapp.urls')), in mysite/myapp/urls.py.
Hope that helps.