所以,我有一個網站,用戶在導航欄上單擊“我的博客”,它會向下滾動到 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 相當于一個視圖。
添加回答
舉報
0/150
提交
取消