项目中的Django- 2nd App,未找到页面

问题描述 投票:0回答:1

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

这里是概述:Project structure

我已在设置已安装的应用中添加了“天气”应用。

在主项目文件夹中的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, 'weather/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('home/', views.PostList.as_view(), name='home'),
    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]

[非常感谢任何抽出宝贵时间阅读并帮助我的人。

python django api url url-routing
1个回答
0
投票

您将必须更改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'),

]

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

© www.soinside.com 2019 - 2024. All rights reserved.