问题描述
带有索引的视图即使使用静态也能完美运行,但其他视图显示错误 500
The code of my urls.py is next:
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('admin/',admin.site.urls),path('',include("home.urls",namespace="home")),path('api/',include("api.urls",namespace = "api")),] +static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
from django.urls import path
from django.contrib.auth import views as auth_views
from home import views
from django.conf import settings
from django.conf.urls.static import static
app_name="home"
urlpatterns=[
path('VerItems/',views.VerItems,name="VerItems"),path('VerAutores/',views.VerAutores,name="VerAutores"),path('Agregarautor/',views.Agregarautor,name="Agregarautor"),path('AgregarItem/',views.AgregarItem,name="AgregarItem"),views.Index.as_view(),name="index"),]
最后。设置文件,我知道 BASEDIR 不是每个网站都应该显示的,但是如果我更改它,索引将不再起作用
from pathlib import Path
import os.path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# Security WARNING: keep the secret key used in production secret!
SECRET_KEY = 'SECRET_KEY'
# Security WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application deFinition
INSTALLED_APPS = [
'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','home','rest_framework',]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','whitenoise.middleware.WhiteNoiseMiddleware',]
ROOT_URLconf = 'examen2.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates','Dirs': [os.path.join(BASE_DIR,'templates')],'APP_Dirs': True,'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},]
Wsgi_APPLICATION = 'examen2.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql_psycopg2',# 'NAME': 'db_exa2',#'USER': 'exa2_user',# 'PASSWORD': 'exa2_user11',# 'HOST': '127.0.0.1',# 'PORT': '5432',# }
#}
import dj_database_url
from decouple import config
DATABASES = {'default':{
'ENGINE': 'django.db.backends.postgresql','NAME': 'dnb74q5rqcp7c','USER': 'cjkzdvkrbdffbf','PASSWORD': '42ce12501b0132835f5ec98a33017cda9e8a32d2e8929b618e7a226e17100705','HOST': 'ec2-34-232-212-164.compute-1.amazonaws.com','PORT': '5432',}}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_ROOT= os.path.join(BASE_DIR,'staticfiles')
STATIC_URL = '/static/'
STATICFILES_Dirs = [
BASE_DIR / "statics/",]
STATICFILES_STORAGE= 'whitenoise.storage.CompressedManifestStaticFilesstorage'
我不认为问题是静态文件和相关的,因为我的索引显示得很好,但老实说不确定,我已经遇到这个问题好几个小时了,现在不知道该怎么办。也许我需要修改一些网址,但在网上找不到,所以...
这是views.py:
rom django.shortcuts import render,redirect
from django.views import generic
from .models import Items,Autor
from api.forms import FormAutor,FormItem
from rest_framework.views import APIView
from rest_framework.response import Response
import requests
# Create your views here.
class Index(generic.TemplateView):
template_name="home/index.html"
def VerItems(request):
url= "http://127.0.0.1:8000/api/ItemsGET/"
response = requests.get(url)
obj = response.json()
context={
"objs":obj
}
return render(request,"home/VerItems.html/",context)
def VerAutores(request):
url= "http://127.0.0.1:8000/api/AutoresGET/"
response = requests.get(url)
obj = response.json()
context={
"objs":obj
}
return render(request,"home/VerAutores.html/",context)
def Agregarautor(request):
form = FormAutor(request.POST)
if request.method == 'POST':
form = FormAutor(request.POST)
if form.is_valid():
nombre = form.cleaned_data['autor_nombre']
anio = form.cleaned_data['autor_anio']
sexo = form.cleaned_data['autor_sexo']
nacionalidad = form.cleaned_data['autor_nacionalidad']
url= "http://127.0.0.1:8000/api/AutoresGET/"
payload = {
"nombre":nombre,"anionacimiento":anio,"sexo":sexo,"nacionalidad":nacionalidad
}
response = requests.post(url,data=payload)
return redirect("home:VerAutores")
form = FormAutor()
context = {
"form":form
}
return render(request,"home/Agregarautor.html/",context)
def AgregarItem(request):
form = FormItem(request.POST)
if request.method == 'POST':
form = FormItem(request.POST)
if form.is_valid():
if form.is_valid():
nombre = form.cleaned_data['Item_nombre']
anio = form.cleaned_data['Item_anio']
valor = form.cleaned_data['Item_valor']
nombreautor = form.cleaned_data['Item_autor']
categoria = form.cleaned_data['Item_categoria']
url= "http://127.0.0.1:8000/api/ItemsGET/"
payload = {
"nombre":nombre,"aniocreacion":anio,"valor":valor,"nombreautor":nombreautor,"categoria":categoria,}
response = requests.post(url,data=payload)
return redirect("home:VerItems")
form = FormItem()
context = {
"form":form
}
return render(request,"home/AgregarItem.html/",context)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)