【问题标题】:django 404 page not found error http://localhost:8000/static/feed.htmldjango 404 页面未找到错误 http://localhost:8000/static/feed.html
【发布时间】:2020-05-26 01:46:56
【问题描述】:

django 正在将 /static/ url 添加到我的路径中,我不知道为什么

这是我的意见.py

from django.shortcuts import render
from django.views.generic import TemplateView, ListView, CreateView
from app.models import SendSMS
from app.forms import SendSMSForm
from django.urls import reverse_lazy
from app.utils import send_twilio_message
from django.conf import settings 
import datetime
# Create your views here.

# Create your models here.

def index(request): 
     return render(request, 'index.html')
def feed(request): 
     return render(request, 'feed.html')
def upload(request): 
     return render(request, 'upload.html')
def messages(request): 
    return render(request, 'messages.html')
def statistics(request): 
    return render(request, 'statistics.html')


class SendSmsCreateView(CreateView):
     model = SendSMS
     form_class = SendSMSForm
     template_name = 'messages.html'
     success_url = reverse_lazy('send_sms')

     def form_valid(self, form):
         number = form.cleaned_data['to_number']
         body = form.cleaned_data['body']
        # call twilio
         sent = send_twilio_message(number, body)
        # save form
        send_sms = form.save(commit=False)
        send_sms.from_number = settings.TWILIO_PHONE_NUMBER
        send_sms.sms_sid = sent.sid
        send_sms.account_sid = sent.account_sid
        send_sms.status = sent.status
        send_sms.sent_at = datetime.datetime.now()
        if sent.price:
            send_sms.price_unit = sent.price_unit
            send_sms.save()

        return super(SendSmsCreateView, self).form_valid(form)      

这是我的应用程序 urls.py

from . import views
from django.contrib import admin
from django.conf.urls import url
from django.urls import path, include
app_name = 'app'

urlpatterns = [
   path('', views.index, name='index'),
   path('', views.feed, name='feed'),
   path('', views.upload, name='upload'),
   path('', views.messages, name='messages'),
   path('', views.statistics, name='statistics'),
]

这是我的项目 urls.py

from django.contrib import admin
from django.conf.urls import url
from django.urls import path, include
from app.views import SendSmsCreateView
url(r'^admin/', admin.site.urls),
# ... your url patterns
path('', include('app.urls', namespace='app')),


url(
    regex=r'^app/$',
    view=SendSmsCreateView.as_view(),
    name='send_sms'
  ),

]

这是我的 index.html 文件

{% load static %}

<!DOCTYPE html>
 <html>
<head>
</head>
<body>
<li class="nav-item">
        <a href=" {% static 'feed.html' %} " class="nav-link">
          <i class="nav-icon fas fa-th"></i>
          <p>
            Feed
            <span class="right badge badge-danger">New</span>
          </p>
        </a>
      </li>
      <li class="nav-item has-treeview">
        <a href=" {% static 'messages.html' %} " class="nav-link">
          <i class="nav-icon fas fa-copy"></i>
          <p>
            Compose Message

          </p>
        </a>

      </li>
      <li class="nav-item has-treeview">
        <a href=" {% static 'upoad.html' %} " class="nav-link">
          <i class="nav-icon fas fa-chart-pie"></i>
          <p>
            Upload CSV
          </p>
        </a>
      </li>

   </body>

【问题讨论】:

    标签: python mysql django url azure-web-app-service


    【解决方案1】:

    一些注意到的故障是

    视图中缺少上下文,如图所示添加一个空括号,它现在将作为上下文服务,稍后您可以添加所需的键和值

    def index(request): 
         return render(request, 'index.html', {})
    

    索引网址错误,需要添加非重叠正则表达式模式

    path('', views.index, name='index'),
    path('feed/', views.feed, name='feed'),
    path('upload/', views.upload, name='upload'),
    path('messages/', views.messages, name='messages'),
    path('statistics/', views.statistics, name='statistics'),
    

    将网址更改为已过时的路径

    path('app/',
        view=SendSmsCreateView.as_view(),
        name='send_sms'
      ),
    

    【讨论】:

      【解决方案2】:

      我所做的是在 html 文件中添加命名空间

      在我使用之前

       <a href=" {% url 'feed.html' %} " class="nav-link active">
      

      上面的没有用,所以我把它改成了

       <a href=" {% url 'app:feed' %} " class="nav-link active">
      

      【讨论】:

        猜你喜欢
        • 2017-06-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 2021-11-21
        • 1970-01-01
        相关资源
        最近更新 更多