I'm new to Django & I'm using version 1.8.3. I'm having trouble with the urls. I've searched everywhere for an answer and can't find one. No matter what I do, I keep getting a "NameError" and when I don't get an error, I just get the index.html page.
Both http://127.0.0.1:8000/ & http://127.0.0.1:8000/questions will render the index.html in the templates directory or throw an error.
I would like http://127.0.0.1:8000/questions to render the home.html in the templates/questions directory and http://127.0.0.1:8000/ to render the index.html in templates/.
I have the model registered in questions/admin.py and the app installed in MyProj/settings.py.
folder structure:
MyProj
|.Python
|_bin
|_include
|_lib
|_src
| |_questions
| | |_ __init__.py
| | |_admin.py
| | |_migrations
| | |_models.py
| | |_tests.py
| | |_urls.py
| | |_views.py
| |
| |_MyProj
| | |_ __init__.py
| | |_settings.py
| | |_urls.py
| | |_views.py
| | |_wsgi.py
| |
| |_static_in_pro
| | |
| | |_our_static
| | |_bootstrap
| | |_css
| | |_fonts
| | |_js
| |
| |_templates
| | |_base.html
| | |_index.html
| | |_errors
| | |_partials
| | |_questions
| | |_home.html
| |
| |_vendor
|
|_static_in_env
|_media_root
|_static_root
MyProj/settings.py
. . .
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#third party apps
'crispy_forms',
'localflavor',
'registration',
#my apps
# 'jobs',
# 'likes',
# 'matches',
# 'newsletter',
# 'profiles',
'questions',
)
. . .
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "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',
],
},
},
]
. . .
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_in_pro", "our_static"),
. . .
MyProj/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from questions import views, urls
from .views import IndexView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('^.*$', IndexView.as_view(), name='index'),
# url(r'^$', include('questions.urls') ), #Changed here
url(r'^questions/$', views.home, name='home'),
#url(r'^questions/$', include(questions.urls)),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MyProj/views.py
from django.shortcuts import render
from django.views.generic.base import TemplateView
from questions import views
class IndexView(TemplateView):
template_name = 'index.html'
questions/urls.py
from django.conf.urls import patterns, url
from questions import views
urlpatterns = [
url(r'^questions$', views.home, name='home'),
]
questions/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question, Answer
class AnswerTabularInline(admin.TabularInline):
model = Answer
class QuestionAdmin(admin.ModelAdmin):
inlines = [AnswerTabularInline]
class Meta:
model = Question
admin.site.register(Question, QuestionAdmin)
admin.site.register(Answer)
questions/views.py
from django.shortcuts import render
from django.http import Http404
from django.shortcuts import render, get_object_or_404, redirect
# Create your views here.
from .models import Question, Answer
def home(request):
return render(request, "questions.home.html", {})
Can someone please tell me what I'm missing? Thanks in advance! :)
Your url pattern index is catching all urls. This means that any url patterns below will be ignored.
url('^.*$', IndexView.as_view(), name='index'),
To match only the index (i.e. http://127.0.0.1:8000/), change it to
url(r'^$', IndexView.as_view(), name='index'),
Note that I've also added the r'' prefix to the regex. It doesn't make any difference in this case, but it's a good habit to use it in your regexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With