質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1328閲覧

Djangoチュートリアルのpollsアプリが 本番サーバで動作しない

bayashiP

総合スコア13

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2020/03/08 09:39

少し前に同じ質問を投稿し、原因不明のまま正常に動作したので解決済みにしたのですが、、、
再発しましたので、再度投稿させていただきます。

前提・実現したいこと

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__
. └─[省略]

発生している問題・エラーメッセージ

http://localhost/polls/

にアクセスすると、次のようなメッセージが表示されます

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

で開発用サーバを起動し、

http://localhost:8000/polls/

にアクセスすると、問題なくページが表示されます。

ただApacheサーバーの方を起動して

http://localhost/polls/

にアクセスすると、上記のエラーとなります。

1度突然正常に動いたことがあったのですが、何が原因かはわかりません。その時、ファイルの中身やディレクトリ構造は一切変えていません。

今回、

httpd -k restart

でApacheサーバを再起動したところ、同様のエラーが再発しました。

コンピュータ自体の再起動をしてみましたが、エラーは消えませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問