我想在这样的模板来显示评论:
{{ url note.note }}
{{ url note.note_by }}
我的问题是我的模板是在另一个目录从那里认为,“请求”的音符。 如何链接到另一个目录视图(或者更具体地说,在note.note函数内部的views.py在其他目录)?
在同一目录作为模板views.py,我链接到这样的模板:
def blah(request):
return render(request, 'site/blah.html')
而在我请求和保存的意见(note.note)的views.py看起来就像这样:
def messages(request):
if request.method == "POST":
new_note_text = request.POST.get('new_note')
new_note = Note()
new_note.note = new_note_text
new_note.note_by = request.user
new_note.note_datetime = timezone.now()
new_note.save()
return render(request, 'theme/messages.html', context)
编辑:
urls.py在同一目录作为模板:
from django.conf.urls import include, patterns, url
from site import views
urlpatterns = patterns('',
url(r'^test$', views.blah, name='blah'),
)
settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'theme',
'site',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'app.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 = 'appen.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'