Django-项目中的第二个应用程序,找不到页面

Django- 2nd App within project, page not found

我无法弄清楚在我的项目名为 'weather' 的第二个应用程序中我做错了什么。第一部分 'home' 工作正常。我这样做是为了让一个空的 url '' 和一个带有 '/home/' 的 url 都映射到主页。即使是我在主页导航栏中创建的 link 也能正常工作,但天气 link 就不行了。我最终收到 'Page not found 404' 错误。

这里是概述: Project structure

我在设置安装的应用程序中包含了应用程序 'weather'。

主项目文件夹中的urls.py文件:

from django.contrib import admin
from django.urls import path, include
from weather import views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
    path('', include('weather.urls')),
    ]

我的天气应用的 urls.py 文件:

from weather import views
from django.urls import path

urlpatterns = [
    path('', views.GetWeather.as_view(), name='weather'),

]

天气应用 views.py:

from django.views import generic
import requests
from django.shortcuts import render
from .models import City
from .forms import CityForm


class GetWeather(generic.ListView):

    queryset = City.objects.order_by('-requested')

    template_name = 'weather.html'

def index(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=c5a079de62bccff63d64ac8989f87e37'

    form = CityForm()

    cities = City.objects.all()

    weather_data = []

    for city in cities:

        r = requests.get(url.format(city)).json()

        city_weather = {
            'city' : city.name,
            'temperature' : r['main']['temp'],
            'description' : r['weather'][0]['description'],
            'icon' : r['weather'][0]['icon'],
        }

        weather_data.append(city_weather)

    context = {'weather_data' : weather_data, 'form' : form}
    return render(request, 'templates/weather.html', context)

天气应用 models.py:

from django.db import models
from django.contrib.auth.models import User

class City(models.Model):
    name = models.CharField(max_length=30)

    requested = models.DateTimeField(auto_now_add = True)

    class Meta:
        ordering = ['-requested']

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'cities'

weather.html 模板:

<!DOCTYPE html>
{% extends "base.html" %}
{% block content %}

<html>
<style>
    body {
        font-family: "Roboto", sans-serif;
        font-size: 18px;
        background-color: rgba(0, 0, 0, .75);
        }

    .head_text{
        color: white;
        }
    .card{
        box-shadow: 0 16px 48px #E3E7EB;
        }
</style>

        <div class="container">
            <div class="row">
                <div class="col-md-8 mt-3 left">
                    {% for city_weather in weather_data %}
                    <div class="card mb-4">
                        <div class="card-body">
                            <h2 class="media-left">
                                <figure class="image is-50x50">
                                    <img src="http://openweathermap.org/img/w/{{ city_weather.icon }}.png" alt="Image">
                                </figure>
                            </h2>
                            <div class="media-content">
                                <div class="content">
                                    <p>
                                        <span class="title">{{ city_weather.city }}</span>
                                        <br>
                                        <span class="subtitle">{{ city_weather.temperature }}° F</span>
                                        <br> {{ city_weather.description }}
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
</html>
{% endblock content %}

这是blog.urls:

from . import views
from django.urls import path

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]

我非常感谢任何花时间阅读本文并帮助我的人。

您必须首先更改 blogs/urls.pyweather/urls.py 匹配项 URL。这取决于您想在基地上看到什么 URL.

# weather/urls.py

from weather import views
from django.urls import path

urlpatterns = [
    path('weather/', views.GetWeather.as_view(), name='weather'),

]
# blog/urls.py

from . import views
from django.urls import path

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('blog/<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]

这应该可以解决,假设您希望在第一页显示天气详细信息。