Django内部服务器错误(仅部分页面)

pdkcd3nj  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(119)

我在玩剪刀石头布。我想使用一个base.html页面,并使用其他页面的主体扩展它。这是工作的2页,但不是为其他两个。我找不到为什么我得到一个内部服务器错误,为什么我的CSS文件不加载。
有人见过或经历过吗??

[21/Aug/2020 09:36:49] "GET /yilmaz/game_view/ii/IQ HTTP/1.1" 200 1474
Internal Server Error: /yilmaz/static/yilmaz/base.css
Traceback (most recent call last):
  File "D:\Studium\it\repo\webproject\yilmaz\web\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Studium\it\repo\webproject\yilmaz\web\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Studium\it\repo\webproject\yilmaz\web\lib\site-packages\django\views\static.py", line 36, in serve
    fullpath = Path(safe_join(document_root, path))
  File "D:\Studium\it\repo\webproject\yilmaz\web\lib\site-packages\django\utils\_os.py", line 17, in safe_join
    final_path = abspath(join(base, *paths))
  File "c:\users\güney\appdata\local\programs\python\python38-32\lib\ntpath.py", line 78, in join
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType
[21/Aug/2020 09:36:49] "GET /yilmaz/static/yilmaz/base.css HTTP/1.1" 500 72104

我的django观点:

from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from random import randint
from . import models
from .models import HighscoreForm #ScoreForm
import uuid

leben = 5 
stimmen = []
punkte = 0
user = ""
context = {}
#Startseite mit Highscore Liste
def startpage(request):
    # context = die letzten 3 Einträge sortiert nach den Punkten(Score)
    context = models.Highscore.objects.order_by("-score")[:3]
    return render(request,"yilmaz/start.html",{"context":context})

# spieler erstellen
def create_player(request):
    
    player = models.Highscore()
    if request.POST:
        form = HighscoreForm(request.POST)
        if form.is_valid():
            player_name = form["name"].value()
            player_country = form["country"].value()
            #player = form.save()
            print("das ist der Player ", player)
            #player.save()
            form = HighscoreForm()
            return redirect(reverse("game_view", kwargs={"player_name":player_name,"player_country":player_country}))
    form = HighscoreForm()
    return render(request,"yilmaz/create_player.html", {"form":form})
    

def set_cookie(response, request, cookie):
    if "game_ssp" in request.COOKIES:
        return response
    else:
        response.set_cookie("game_ssp", cookie, max_age=2*60*60)
        return response




def game_view(request, player_name,player_country):
    if "game_ssp" not in request.COOKIES:
        cookie = str(uuid.uuid4().hex)
        return set_cookie(redirect(reverse("game_view", kwargs={"player_name":player_name,"player_country":player_country})),request,cookie)
    else:
        cookie = request.COOKIES['game_ssp']     
        print(cookie)
    player_name = player_name
    #highscore = models.Highscore(name=player_name, cookie=cookie, country=player_country)
    #highscore.save()
    #print(highscore.name,highscore.cookie, "Spieler name, der übergeben wurde")
        
    #
    global leben
    global punkte
    global user
    global stimmen
    global context
    
    #context["Leben"] = leben
    context["Punkte"] = int(punkte)
    #form = ScoreForm()
    #player = request.POST.get("spieler_input")
    #print(player)
    if "wahl_abschicken" in request.POST:
        
        
        
        #computer  & player stimme wird erstellt
        #print("Eingabe ist", player)
        computer = randint(0,2)
        player = int(request.POST.get("answer"))
        # Spielsituation wird duchgegangen
        if computer == 0:
            context["computer"] = "Schere"
        elif computer == 1:
            context["computer"] = "Stein"
        else:
            context["computer"] = "Papier"
        if player == 0:
            context["wahl"] = "Schere"
        elif player == 1:
            context["wahl"] = "Stein"
        else:
            context["wahl"] = "Papier"
        if computer  > player:
            context["ergebnis"] = "Computer gewinnt"
            leben -= 1
            # leben auf 0, Spiel wird neu gestartet
            if leben == 0:
                    highscore = models.Highscore(name=player_name, score=punkte, cookie=cookie,country=player_country)
                    print(highscore)
                    highscore.save()
                    leben = 5
                    stimmen = []
                    punkte = 10 
                    context = models.Highscore.objects.order_by("-score")[:10]

                    return render(request, "yilmaz/game_end.html",{"context":context}) 
            
        elif computer == player:
            context["ergebnis"] = "Pat"
        else:
            context["ergebnis"] = "Player gewinnt"
            punkte += 1
        # prints zum testen
        #print("Computer",computer)
        #print("Leben",leben)
        context["Leben"] = leben
        stimmen.append(context)
        return redirect(reverse("game_view", kwargs={"player_name":player_name,"player_country":player_country}))
    print(context)
    print(stimmen)
    return render(request, "yilmaz/game.html",{"context":context})

Urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

from . import views

urlpatterns = [
    path("", views.startpage, name="startpage"),      
    path("create_player", views.create_player, name="create_player"),
    path("game_view/<str:player_name>/<str:player_country>", views.game_view, name="game_view"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这是我的基本.html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://unpkg.com/nes.css@2.3.0/css/nes.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet"> 
    <link href="../../static/yilmaz/css/base.css" rel="stylesheet">
    <link href="../../static/yilmaz/favicon.png" rel="shortcut icon" type="image/png">
    <title>Schere, Stein, Papier</title>
</head>
<body>
<div class="container">
    <div class="side">dark mode</div>
    <main>
        {% block content %}

        INSERT YOUR CONTENT HERE

        {% endblock content %}
    </main>
    <div class="side"></div>
</div>
</body>
</html>

这是我的游戏.html:

{% extends 'yilmaz/base.html' %}
{% load static %}
{% block content %}

  <h1>Schere, Stein, Papier</h1></br>
  <form method="POST">
    <p>Spiele gegen den Computer</p>
  {% csrf_token %}
      {% for key, value in context.items %}
    <p>{{key}}: {{value}}</p>
      
  {% endfor %}
    <label>
      <input type="radio" class="nes-radio" name="answer" checked value="0"/>
      <span>Schere</span>
    </label>
    <br>
    <label>
      <input type="radio" class="nes-radio" name="answer" checked value="1"/>
      <span>Stein</span>
    </label>
    <label>
      <input type="radio" class="nes-radio" name="answer" checked value="2"/>
      <span>Papier</span>
    </label>
    <input type="submit" name="wahl_abschicken" value="wahl_abschicken">
    <br>
  </form>
{% endblock content %}

css:(borders仅用于开发)

body {
    font-family: "Press Start 2P", Arial;
    box-sizing: border-box;
    margin: 50px 0;
    border : 5px solid red;
    font-size: 10px;
    
}
.container{
    display: flex;
    border: 2px dashed navajowhite ;
}

.side {
    height: 1000px;
    width: 25%;
    color: yellow;
    border: 2px dotted palegreen;
}

main{
    text-align: center;
    width: 50%;
}
llew8vvj

llew8vvj1#

内部服务器错误通常发生在settings.py中的DEBUG = False。尝试在你的开发环境中创建DEBUG = True,并查看完整的django错误解释。在我看来,这是调试您的问题的最佳方法。

相关问题