亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何分別為 Index.html 的一部分創建視圖?

如何分別為 Index.html 的一部分創建視圖?

汪汪一只貓 2022-06-14 16:51:16
所以,我有一個網站,用戶在導航欄上單擊“我的博客”,它會向下滾動到 index.html 的那個部分(例如 8000/#blog-section)。我已經在索引頁面上有了聯系表格?,F在我正在嘗試創建博客邏輯以呈現三篇博客文章,但它顯示為空白?我可以為同一個“index.html”頁面創建一個單獨的視圖嗎?Views.pydef index_view(request):    posts = Post.objects.all().order_by('-created_on')    context ={        'posts': posts,        'name': name,    }    form = ContactForm()    if request.method == 'POST':        form = ContactForm(data=request.POST)        if form.is_valid():            # Send email goes here            name = request.POST.get('name')            subject = request.POST.get('subject')            email = request.POST.get('email')            message = request.POST.get('message')            template = get_template('contact_form.txt')            context = {                'subject': subject,                'email' : email,                'message' : message            }            content = template.render(context)            email = EmailMessage(                "Message from Portfolio",                content,                "New inquiry | Portfolio" + '',                ['[email protected]'],                headers = {'Reply to': email}            )        email.send()        return HttpResponseRedirect("/")    return render(request, 'index.html', {'form': form}, context)網址.py /博客from django.urls import pathfrom . import viewsurlpatterns = [    path("", views.blog_index, name="blog_index"),    path("<int:pk>/", views.blog_detail, name="blog_detail"),    path("<category>/", views.blog_category, name="blog_category"),]Urls.py / 投資組合from django.conf import settingsfrom django.conf.urls.static import staticfrom django.contrib import adminfrom django.urls import path, includefrom .views import index_view, post_viewurlpatterns = [    path('admin/', admin.site.urls),    path('tinymce/', include('tinymce.urls')),    path('', index_view),    path('post/', post_view),    path("#blog-section/", include("blog.urls")),]
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

出于某種原因,您將帖子放在與表單不同的字典中。這不是它的工作原理。將所有上下文放在一個字典中。


context = {

    'posts': posts,

    'name': name,

    'form': form

}


return render(request, 'index.html', context)

并回答您的問題:不,您不能(至少在沒有使用 Ajax 構建頁面的情況下不能)。一個 URL 相當于一個視圖。


查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號