简体   繁体   English

Django - 静态 main.css 的递归错误

[英]Django - Recursion Error with static main.css

Running into a max recursion error when hitting my static main.css file in django.在 django 中访问我的静态 main.css 文件时遇到最大递归错误。 Need help identifying the issue and how to correct it.需要帮助确定问题以及如何纠正它。

Had no issues when my login/sign up pages were within my single dbManagementApp.当我的登录/注册页面在我的单个 dbManagementApp 中时没有问题。 After adding those to a new app (accounts) then adding the templates into a specific folder for each app, I received the below error message.将这些添加到新应用程序(帐户)然后将模板添加到每个应用程序的特定文件夹后,我收到以下错误消息。

Debugger调试器

In template ###, error at line 7

maximum recursion depth exceeded while calling a Python object
1   <!DOCTYPE html>
2   <html lang="en" dir="ltr">
3       <head>
4           <meta charset="utf-8">
5           <title>HOME</title>
6           {% load static %} <!-- load the static folder for css-->
7           <link rel="stylesheet" href="{% static 'main.css' %}">

Traceback追溯

File "C:\Users\lukep\anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 766, in __init__
    self.literal = float(var)
ValueError: could not convert string to float: "'main.css'"

My main urls.py:我的主要 urls.py:

from django.contrib import admin
from django.urls import path, include
from dbManagementApp import dbmviews
from accounts import accviews

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('dbManagementApp.dbmurls')),
    path('accounts/', include('accounts.accurls'))
]

dbManagementApp/dbmurls.py dbManagementApp/dbmurls.py

from django.urls import path
from . import dbmviews
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('', dbmviews.home, name='home'),
    path('aboutus/', dbmviews.aboutUs, name='aboutUs'),
    path('pricing/', dbmviews.pricing, name='pricing'),
    path('contact/', dbmviews.contact, name='contact'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

dbManagementApp/dbmviews.py dbManagementApp/dbmviews.py

from django.shortcuts import render, redirect
from django.views import View
from .models import Doc
from .forms import DocumentForm
from django.contrib import messages
from django.http import HttpResponse
from django.http import JsonResponse

# Create your views here.
def home(request):
    return render(request, 'dbmapp/index.html')

templates/dbmapp/index.html模板/dbmapp/index.html

<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>HOME</title>
        {% load static %} <!-- load the static folder for css-->
        <link rel="stylesheet" href="{% static 'main.css' %}">

accounts/accurls.py帐户/accurls.py

from django.urls import path
from . import accviews

urlpatterns = [
    path('register/', accviews.register, name='register'),
    path('login/', accviews.login, name='login')
]

accounts/accviews.py帐户/accviews.py

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = UserCreationForm()
    return render(request, 'registration/register.html', {'form':form})

def login(request):
    return render(request, 'registration/login.html')

I'm not fully understand your problem, but I'd change 3 things我不完全理解你的问题,但我会改变 3 件事

  • {% load static %} should be at the top of the html file {% load static %}应该在 html 文件的顶部
  • Move path('', include('dbManagementApp.dbmurls')), first (before admin)移动path('', include('dbManagementApp.dbmurls')),首先(在 admin 之前)
  • Remove dir attribute in html.删除 html 中的dir属性。

Just a question, are you using {% extends 'whatever.html' %} ?, it cause error from duplicating tags.只是一个问题,你在使用{% extends 'whatever.html' %}吗?它会导致重复标签出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM