Django - Url Dispatching - 找不到页面

时间:2018-01-02 04:48:32

标签: django python-3.x django-urls

我很困惑为什么URL调度程序试图寻找一个空路径虽然为它指定了一个url?这是否意味着它无法找到指定的URL,因此试图找到默认值。

当我尝试POST并且HttpResponseRedirect搜索空路径而不是遵循指定路径时,会发生这种情况。假设另一个。

使用Django版本:2.0

提前致谢!

main / urls.py(ROOT_URLCONF)

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

urlpatterns = [
   path('admin/', admin.site.urls),
   path('app/', include('shorterner.urls'))
]

shortner / urls.py

from django.urls import path
from . import views

urlpatterns = [
  path('request/', views.RequestView.as_view(), name="request"),
  path('list/', views.IndexView.as_view(), name="list")
]

shorterner / views.py

from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.conf import settings
from django.urls import reverse
from django.views import View, generic
from .models import Urls

import requests
import json

from .forms import SubmitUrlForm

class RequestView(View):
  form_class = SubmitUrlForm
  initial = { 'url': ''}
  template_name = "shorterner/request.html"
  context_object_name = 'url'

  def form_valid(self, form):
    return super().form_valid(form)

  def get(self, request, *args, **kwargs):
    form = self.form_class(initial=self.initial)
    return render(request, self.template_name, {'form': form})

  def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST)
    if form.is_valid():
      input_url = form.cleaned_data['url']
      short_url = google_url_shorten(input_url)

      print(input_url)
      print(short_url)

      new_url = Urls.create(short_url, input_url)
      new_url.save()

      return HttpResponseRedirect('/app/request/')
    return render(request, self.template_name, {'form': form})

shorterner /模板/ shorterner / request.html

{% extends 'shorterner/base.html' %}

{% block content %}

    <form action="/" method="POST">
      {% csrf_token %}
      <input type="text" name="url" placeholder="Url..." required/>        
      <input type="submit" value="Submit"/>
    </form>

{% endblock %}

问题的屏幕截图 Error Page

1 个答案:

答案 0 :(得分:0)

您似乎正在将表单数据发送到此路径/。但是你还没有在main/urls.py中定义该路线。