【问题标题】:NoReverseMatch at /main/insert_num/ DjangoNoReverseMatch 在 /main/insert_num/ Django
【发布时间】:2020-08-03 13:55:32
【问题描述】:

我正在尝试制作一个 django 网络应用程序,该应用程序具有一个要求用户输入电话号码并将该号码存储在 postgres 数据库中的表单。以下代码给了我错误:

NoReverseMatch 在 /main/insert_num/

找不到''的反向。 '' 不是有效的视图函数或模式名称。

我不知道是什么问题,有人可以帮忙吗?

index.html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Test Form 1</title>
</head>
<body>
  <form action="{% url 'insert_my_num' %}" method="post" autocomplete="off">
    {% csrf_token %}
    <!-- {{ form.as_p }} -->
    <input type="submit" value="Send message">
  </form>
</body>
</html>

forms.py

from django import forms
from phone_field import PhoneField
from main.models import Post

class HomeForm(forms.ModelForm):
    phone = PhoneField()

    class Meta:
        model = Post
        fields = ('phone',)

models.py

from django.db import models
from phone_field import PhoneField


class Post(models.Model):
    phone = PhoneField()

ma​​in/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('insert_num/', views.insert_my_num,name='insert_my_num')
]

project/urls.py

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

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

views.py

def insert_my_num(request: HttpRequest):
    phone = Post(request.POST.get('phone'))
    phone.save()
    return redirect('')

【问题讨论】:

    标签: python django postgresql post web-applications


    【解决方案1】:

    您的views.py 有点偏离 - 您没有在任何地方呈现您的表单。我起草了一个快速应用程序(我认为它可以满足您的需求) - 如果可行,请告诉我:

    main/templates/index.html

    在这里,我只是将表单的操作设置为 ""(这就是您需要的全部)并取消注释 form.as_p

    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Test Form 1</title>
    </head>
    <body>
      <form action="" method="post" autocomplete="off">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Send message">
      </form>
    </body>
    </html>
    

    main/views.py

    注意这里的区别,我们正在测试请求类型并根据传入的请求类型采取适当的措施。如果是 POST 请求,我们会处理表单数据并保存到数据库中。如果没有,我们需要显示一个空白表单供用户填写。

    from django.shortcuts import render, redirect
    from .forms import HomeForm
    
    
    def insert_my_num(request):
        # Check if this is a POST request
        if request.method == 'POST':
            # Create an instance of HomeForm and populate with the request data
            form = HomeForm(request.POST)
            # Check if it is valid
            if form.is_valid():
                # Process the form data - here we're just saving to the database
                form.save()
                # Redirect back to the same view (normally you'd redirect to a success page or something)
                return redirect('insert_my_num')
        # If this isn't a POST request, create a blank form
        else:
            form = HomeForm()
    
        # Render the form
        return render(request, 'index.html', {'form': form})
    

    让我知道这是否有效!

    【讨论】:

    • 很高兴能帮上忙!
    猜你喜欢
    • 2016-07-20
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2017-05-21
    相关资源
    最近更新 更多