为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哔哔one 2021-11-30 10:22:43
所以我试图从我的模型中渲染数据,user_profile并且出于某种原因,当我尝试将create_view_two视图渲染到我的项目根目录时home.html,页面不会渲染视图。但是,如果我的create_view_two视图指向不同的页面,它就可以工作。下面的例子:user_profile/urls.pyurlpatterns = [    path("test/", create_view_two, name='home'),]user_profile/views.py   return render(request, "test.html", context)我想将这个观点指向我的项目的根源,这localhost:8000不是什么不同的东西。我怎样才能做到这一点?任何帮助都非常感谢。干杯user_profile/views.pyfrom django.http import HttpResponse, HttpResponseRedirectfrom django.http import HttpResponseNotFoundfrom django.shortcuts import get_object_or_404from django.shortcuts import render, redirectfrom django.conf import settingsfrom .forms import HomeFormfrom .models import Listingfrom users.models import CustomUserdef 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.pyfrom django.conf.urls import urlfrom . import viewsfrom django.urls import path, includefrom django.conf import settingsfrom .views import create_view, create_view_twourlpatterns = [    path('myaccount/', create_view, name='myaccount'),    path('', create_view_two, name='home'),]master_application/urls.pyfrom django.conf.urls import urlfrom django.contrib import adminfrom django.urls import pathfrom django.urls import path, includefrom django.views.generic.base import TemplateViewfrom django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [    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')),    ]
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

在应用程序user_profile中,您声明的是视图create_view_two,使用以下内容:


user_profile/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您的项目目录中使用如下内容:


myapp/urls.py

from django.contrib import admin

from django.urls import path, include


urlpatterns = [

    path("admin/", admin.site.urls),

    path("", include("user_profile.urls")),

]

然后,您将create_view_two在localhost:8000/.


查看完整回答
反对 回复 2021-11-30
  • 1 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信