在Django中添加联系人表单时找不到Contact_form

时间:2018-02-17 15:04:32

标签: python django

我在尝试在Django中添加联系表单时找不到消息联系表单。我遵循了这个指南。 https://atsoftware.de/2015/02/django-contact-form-full-tutorial-custom-example-in-django-1-7/这应该有效。

我现在收到一条新的错误消息

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/contact/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/
contact ^$
^ ^$ [name='index']
^blog/
^writing/
The current path, contact/, didn't match any of these.

Settings.py

import os

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'personal',
'blog',
'writing',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'contact',

]

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 = 'myproject.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    '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 = 'myproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.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/dev/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/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
EMAIL_USE_TLS = True  
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_PORT = 587  
EMAIL_HOST_USER = '********@gmail.com'  
EMAIL_HOST_PASSWORD = '****'   # set environ yourself

ADMINS = (
  ('your_name', 'your_email'),   # email will be sent to your_email
)

MANAGERS = ADMINS

(已编辑添加完整设置)

root urls。

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^contact', include('contact.urls')),
url(r'^', include('personal.urls')),
url(r'^blog/', include('blog.urls')),
url(r'^writing/', include('writing.urls')),

]

联系urls.py

from django.conf.urls import url, include
from django.contrib import admin
from . import views


urlpatterns = [

url(r'^$', views.index),

]

联系views.py

from django.shortcuts import render

def index(request):
return render(request, 'contact/contact_form.html')

请帮忙。

2 个答案:

答案 0 :(得分:0)

您已将模块命名为contact而非contact_form(正如教程中所示)。

因此,当您将contact_form添加到INSTALLED_APPS设置时,它无法找到该模块。

您已在contact中拥有模块INSTALLED_APPS,因此只需从列表中删除contact_form即可。

答案 1 :(得分:0)

我使用本指南{{3}}&我不得不改变它以便开始工作。