python - Django - URL 未在根主页中呈现 View

标签 python django

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

但是,如果我有指向不同页面的 create_view_two View ,它就可以工作。

示例如下:

user_profile/urls.py

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

user_profile/views.py

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

我想将此 View 指向我的项目的根目录,即 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 %}  

最佳答案

在您声明为 View create_view_two 的应用 user_profile 中,使用以下命令:

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")),
]

然后,您将在 localhost:8000/ 上呈现 create_view_two

关于python - Django - URL 未在根主页中呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55056435/

相关文章:

python - 优化用于从预定义范围确定合格分数的算法

python - 如何在pycharm上安装tkinter

python - 如何动态更新QTextEdit

Python setuptools 符号链接(symbolic link)和自定义安装扩展

python - 类型错误 : post() missing 1 required positional argument

python - 具体模型阅读 xlsx 文档 Pyomo

python-3.x - 从 python 请求上传时,Django 文件对象始终为 0 字节

android - 未设置 HTTPUrlConnection 中的 HTTP POST 数据?

python - 如何知道是否在 python - django 中选中了复选框?

javascript - 使用 django 在 JS 中获取和使用 python 嵌套列表