猿问

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

所以我试图从我的模型中渲染数据,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')),    

]


哔哔one
浏览 150回答 1
1回答

慕桂英3389331

在应用程序user_profile中,您声明的是视图create_view_two,使用以下内容:user_profile/urls.pyfrom django.urls import pathfrom django.views.generic import TemplateViewfrom . import viewsapp_name = "user_profile"urlpatterns = [    path("", views.create_view_two, name="home"),]然后在urls.py您的项目目录中使用如下内容:myapp/urls.pyfrom django.contrib import adminfrom django.urls import path, includeurlpatterns = [    path("admin/", admin.site.urls),    path("", include("user_profile.urls")),]然后,您将create_view_two在localhost:8000/.
随时随地看视频慕课网APP

相关分类

Python
我要回答