前提・実現したいこと
Djangoを用いたwebサーバの構築を勉強しています。
ひとまずDjangoのチュートリアル1~7の通りにアプリを作り、Apacheの本サーバ上で動かしてみようとしたところ、エラーが出てしまい困っています。
どうすれば解消されるでしょうか?
環境・使用ツール
- Windows 10
- Python 3.8.2
- Django 3.0.3
- Apache 2.4.41
ディレクトリ構造
C:\WebServer
│ db.sqlite3
│ manage.py
│
├─polls
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ init.py
│ │
│ ├─migrations
│ │ └─[省略]
│ │
│ ├─templates
│ │ └─polls
│ │ detail.html
│ │ index.html
│ │ results.html
│ │
│ └─__pycache__
│ └─[省略」
│
├─static
│ ├─admin
│ │ └─ [省略]
│ └─WebServer
│ ├─css
│ │ index.css
│ │
│ └─js
│
└─WebServer
. │ asgi.py
. │ settings.py
. │ urls.py
. │ views.py
. │ wsgi.py
. │ init.py
. │
. ├─templates
. │ └─WebServer
. │ index.html
. │
. └─__pycache__
. └─[省略]
発生している問題・エラーメッセージ
にアクセスすると、次のようなメッセージが表示されます
ValueError at /polls/ set_wakeup_fd only works in main thread ----------------------------- Request Method: GET Request URL: http://localhost/polls/ Django Version: 3.0.3 Exception Type: ValueError Exception Value: set_wakeup_fd only works in main thread Exception Location: c:\users\XXXX\appdata\local\programs\python\python38\lib\asyncio\proactor_events.py in __init__, line 632 Python Executable: C:\Apache24\bin\httpd.exe Python Version: 3.8.2 Python Path: ['c:\WebServer', 'C:\Users\XXXX\AppData\Local\Programs\Python\Python38\python38.zip', 'c:\users\XXXX\appdata\local\programs\python\python38\DLLs', 'c:\users\XXXX\appdata\local\programs\python\python38\lib', 'C:\Apache24\bin', 'c:\users\XXXX\appdata\local\programs\python\python38', 'c:\users\XXXX\appdata\local\programs\python\python38\lib\site-packages'] Server time: Sun, 1 Mar 2020 02:48:34 +0900 ----------------------------- Error during template rendering In template c:\WebServer\polls\templates\polls\index.html, error at line 1 set_wakeup_fd only works in main thread
該当のソースコード
[ WebServer/polls/templates/polls/index.html ]
ここの1行目でエラーが発生しているみたいです
{% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %}
.
.
[ WebServer/polls/views.py ]
チュートリアルの状態から変更していません。
from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views import generic from django.utils import timezone from .models import Choice, Question class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list' def get_queryset(self): return Question.objects.filter( pub_date__lte=timezone.now() ).order_by('-pub_date')[:5] class DetailView(generic.DetailView): model = Question template_name = 'polls/detail.html' def get_queryset(self): """ Excludes any questions that aren't published yet. """ return Question.objects.filter(pub_date__lte=timezone.now()) class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html' def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { 'question': question, 'error_message' : "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results',args=(question_id,)))
.
.
[ WebServer/WebServer/settings.py ]
""" Django settings for WebServer project. Generated by 'django-admin startproject' using Django 3.0.3. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['localhost'] # Application definition INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] 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', ] ROOT_URLCONF = 'WebServer.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'WebServer/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 = 'WebServer.wsgi.application' # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/3.0/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.0/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Tokyo' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/'
Apacheの httpd.conf には、以下の内容を追記しました
Define PROJECT_DIR "c:/WebServer" LoadFile "c:/users/XXXX/appdata/local/programs/python/python38/python38.dll" LoadModule wsgi_module "c:/users/XXXX/appdata/local/programs/python/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd" WSGIPythonHome "c:/users/XXXX/appdata/local/programs/python/python38" WSGIScriptAlias / "${PROJECT_DIR}/WebServer/wsgi.py" WSGIPythonPath ${PROJECT_DIR} <Directory "${PROJECT_DIR}/WebServer"> <Files wsgi.py> Require all granted </Files> </Directory> # # Setting for static files # Alias /static/ "${PROJECT_DIR}/static/" <Directory "${PROJECT_DIR}/static"> Require all granted </Directory>
試したこと
py manage.py runserver
で開発用サーバを起動し、
にアクセスすると、問題なくページが表示されます。
ただApacheサーバーの方を起動して
にアクセスすると、上記のエラーとなります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。