Django-URLS不在根主页中呈现视图

时间:2019-03-08 03:48:20

标签: python django

因此,我试图从名为user_profile的模型中渲染数据,并且出于某种原因,当我尝试将create_view_two视图渲染到名为home.html的项目的根目录时, ,页面将不会呈现视图。

但是,如果我的create_view_two视图指向另一个页面,则可以正常工作。

以下示例:

user_profile / urls.py

urlpatterns = [
    path("test/", create_view_two, name='home'),
]

user_profile / views.py

   return render(request, "test.html", context)

我想将此视图指向项目的根目录,即localhost:8000,没有什么不同。我该怎么办?

很高兴得到任何帮助。

欢呼

user_profile / views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.conf import settings
from .forms import HomeForm
from .models import Listing
from users.models import CustomUser


def create_view_two(request):
    form = HomeForm(request.POST or None, request.FILES or None,)
    user_profile = Listing.objects.all()
    user = request.user

    context = {
        'form': form, 'user_profile': user_profile
    }

    return render(request, "home.html", context)

user_profile / urls.py

from django.conf.urls import url
from . import views
from django.urls import path, include
from django.conf import settings
from .views import create_view, create_view_two

urlpatterns = [
    path('myaccount/', create_view, name='myaccount'),
    path('', create_view_two, name='home'),
]

master_application / urls.py

from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', TemplateView.as_view(template_name='home.html'), name='home'),
    path('admin/', admin.site.urls),
    path('', include('user_profile.urls')),
    path('', include('users.urls')),
    path('', include('django.contrib.auth.urls')),    
    path('users/', include('users.urls')),
    path('users/', include('django.contrib.auth.urls')),
    path('', include('myapp.urls')),    
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

myapp / urls.py

from django.urls import path, include
from django.conf.urls import url
from django.conf import settings
from . import views
from .views import HomePageView, MyAccountView, AboutPageView, PrivacyPageView, ContactPageView

urlpatterns = [
    path('', HomePageView.as_view(), name='home'),    
    path('about/', AboutPageView.as_view(), name='about'),
    path('privacy/', PrivacyPageView.as_view(), name='privacy'),
    path('contact/', ContactPageView.as_view(), name='contact'),
    path('myaccount/', MyAccountView.as_view(), name='myaccount'),
]

home.html

 {% for profile_two in user_profile%} 
<p>{{ profile_two.rank }}</p>
 {% endfor %}  

1 个答案:

答案 0 :(得分:2)

在您声称是视图null的应用StartToMonitor中,使用以下命令:

user_profile / urls.py

user_profile

然后在项目目录内的create_view_two中使用以下命令:

myapp / urls.py

from django.urls import path
from django.views.generic import TemplateView

from . import views

app_name = "user_profile"

urlpatterns = [
    path("", views.create_view_two, name="home"),
]

然后,您将在urls.py处渲染from django.contrib import admin from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("", include("user_profile.urls")), ]