似乎无法让Django在网页中显示正确的内容

时间:2020-08-25 00:17:44

标签: python html css django

我正在使用Django创建一个Web应用程序。我希望该应用程序有4个部分,即首页,文化,娱乐和技术。主页将显示所有文章,而其他页面将显示与该类别相关的文章。我为所有文章创建了一个类。一些类对象是:类别,标题,作者,摘要。在views.py中,对于主页,我创建了一个函数以随机顺序显示所有文章。然后,我又创建了3个按类别显示文章的函数。问题是,主页是唯一可以正常工作的页面。我在导航栏中创建了所有4个页面的链接,所有4个链接都返回到我的主页,而不是我创建的类别页面。我为主页或all_blogs编写的功能是唯一起作用的功能。当我对其进行修改时,它仅显示特定类别中的文章,但它可以工作,但是当我尝试创建新功能和新页面时,它仅显示主页。

views.py

from django.shortcuts import render, get_object_or_404
from .models import Blog


def all_blogs(request):
    blogs = Blog.objects.order_by('?')
    return render(request, 'blog/all_blogs.html', {'blogs': blogs})


def Entertainment(request):
    blogs = Blog.objects.exclude(category='Technology, Culture')
    return render(request, 'blog/Entertainment.html', {'blogs': blogs})


def Technology(request):
    blogs = Blog.objects.exclude(category='Entertainment, Culture')
    return render(request, 'blog/Technology.html', {'blogs': blogs})


def Culture(request):
    blogs = Blog.objects.exclude(category='Entertainment, Technology')
    return render(request, 'blog/Culture.html', {'blogs': blogs})


def pageOne(request, blog_id):
    blog = get_object_or_404(Blog, pk=blog_id)
    return render(request, 'blog/pageOne.html', {'blog': blog})


def all_blogsT(request):
    blogs = Blog.objects.order_by('category')
    return render(request, 'blog/pageOne.html', {'blogs': blogs})

all_blogs.html

{% extends 'blog/navBarFooter.html' %}
{% block content %}
<br>
<br>
{% for blog in blogs %}
<div class="col-lg-12 col-md-8">
    <img src="{{ blog.summaryImage.url }}" height=190 width=350>
    <p>{{ blog.category }}</p>
    <a href="{% url 'pageOne' blog.id %}">
        <h2>{{ blog.title }}</h2>
    </a>
    <p>By: {{ blog.by }} | {{ blog.date|date:'M d Y'|upper }}</p>
    <p>{{ blog.summary }}</p>
    <hr color="black">
</div>
{% endfor %}

{% endblock %}

Entertainment.html

{% extends 'blog/navBarFooter.html' %}
{% block content %}
<br>
<br>
{% for blog in blogs %}
<div class="col-lg-12 col-md-8">
    <img src="{{ blog.summaryImage.url }}" height=190 width=350>
    <p>{{ blog.category }}</p>
    <a href="{% url 'pageOne' blog.id %}">
        <h2>{{ blog.title }}</h2>
    </a>
    <p>By: {{ blog.by }} | {{ blog.date|date:'M d Y'|upper }}</p>
    <p>{{ blog.summary }}</p>
    <hr color="black">
</div>
{% endfor %}

{% endblock %}

navBarFooter.html

<!doctype html>
<html lang="en">
{% load static %}
<!-- **********NAVBAR**************************** -->

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css2?family=Russo+One&display=swap" rel="stylesheet">

    <link rel="stylesheet" type="text/css" href="{% static 'blog/xworld.css' %}">

</head>

<body>
    <nav class=" navbar navbar-expand-sm bg-dark navbar-dark justify-content-end">
        <a class="navbar-brand" href="{% url 'all_blogs' %}">home.com</a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'Culture' %}">Culture</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'Entertainment' %}">Entertainment</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'Technology' %}">Technology</a>
                </li>
        </div>
    </nav>

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from blog import views
    
urlpatterns = [
   

     path('admin/', admin.site.urls),
        path('', views.all_blogs, name='all_blogs'),
        path('entertainment/', views.Entertainment, name='Entertainment'),
        path('technology/', views.Technology, name='Technology'),
        path('culture/', views.Culture, name='Culture'),
        path('<int:blog_id>/', views.pageOne, name='pageOne'),
        ]
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

2 个答案:

答案 0 :(得分:0)

您的网址不能全部与您找到的第一个网址all_blogs相同。给您的网址一个唯一的路径,例如:

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.all_blogs, name='all_blogs'),
    path('entertainment/', views.Entertainment, name='Entertainment'),
    path('technology/', views.Technology, name='Technology'),
    path('culture/', views.Culture, name='Culture'),
    path('blog/<int:blog_id>/', views.pageOne, name='pageOne'),
]

答案 1 :(得分:0)

添加类似的内容

    class PhoneAuth : AppCompatActivity() {
        private var verificationIdGlobal: String? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_phone_auth)
        b_phone_send.setOnClickListener {
        val phoneNumber = et_phone_phone.text.toString().trim()

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
        phoneNumber, // Phone number to verify
        60, // Timeout duration
        TimeUnit.SECONDS, // Unit of timeout
        this, // Activity (for callback binding)
        callbacks) // OnVerificationStateChangedCallbacks
}
    b_phone_sign_in.setOnClickListener {
        val verificationCode = et_phone_verification_code.text.toString().trim()
    verificationIdGlobal?.let {
        val credential = PhoneAuthProvider.getCredential(it, verificationCode)
        addPhoneNumber(credential)
    }
}}

private val callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
    override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) {
        phoneAuthCredential.let {
            addPhoneNumber(phoneAuthCredential)
        }
        TODO("Not yet implemented")
    }

    override fun onVerificationFailed(exception: FirebaseException) {
        Log.d("phone", "$exception")
        TODO("Not yet implemented")
    }

    override fun onCodeSent(verificationId: String, token: PhoneAuthProvider.ForceResendingToken) {
        super.onCodeSent(verificationId, token)
        verificationIdGlobal = verificationId
    }
}

private fun addPhoneNumber(phoneAuthCredential: PhoneAuthCredential){
    FirebaseAuth.getInstance().currentUser?.updatePhoneNumber(phoneAuthCredential)?.addOnCompleteListener {task ->
        if (task.isSuccessful){
            Log.d("Phone", "task successful")
        }
        else{
            Log.d("Phone", "${task.exception}")
        }
    }
}