Django - Ошибка рекурсии со статическим файлом main.css

Столкнулся с ошибкой максимальной рекурсии при обращении к моему статическому файлу main.css в django. Нужна помощь в определении проблемы и как ее исправить.

У меня не было проблем, когда мои страницы входа/подписки находились в моем единственном dbManagementApp. После добавления этих страниц в новое приложение (учетные записи), затем добавления шаблонов в определенную папку для каждого приложения, я получил следующее сообщение об ошибке.

Отладчик

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'"

Мой основной 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

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

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

<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

from django.urls import path
from . import accviews

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

accounts/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')

Я не совсем понимаю вашу проблему, но я бы изменил 3 вещи

  • {% load static %} должен находиться в верхней части html-файла
  • .
  • Переместите path('', include('dbManagementApp.dbmurls')), на первое место (перед admin)
  • Удалите атрибут dir в html.

Просто вопрос, используете ли вы {% extends 'whatever.html' %}?, это вызывает ошибку из-за дублирования тегов.

Надеюсь, что-то из этого поможет.

Вернуться на верх