首页 > 解决方案 > Django-项目中的第二个应用程序,找不到页面

问题描述

我无法弄清楚我在我的项目名为“天气”的第二个应用程序中做错了什么。第一部分,“家”,工作正常。我这样做是为了让一个空的 url '' 和一个带有 '/home/' 的 url 都映射到主页。即使我在导航栏中创建的指向主页的链接也有效,但天气链接并非如此。我最终收到“找不到页面 404”错误。

这是一个概述: 项目结构

我在设置安装的应用程序中包含了应用程序“天气”。

这里是主项目文件夹中的 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'),
]

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

标签: pythondjangoapiurlurl-routing

解决方案


您将不得不更改其中一个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'),
]

这将解决,假设您想要在第一页上的天气详细信息。


推荐阅读