NoReverseMatch 在 /main/insert_num/ Django

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


/main/insert_num/ 处的 NoReverseMatch


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


而且我无法弄清楚问题是什么,有人可以帮忙吗?


索引.html


<html>

<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>

表格.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',)

模型.py


from django.db import models

from phone_field import PhoneField



class Post(models.Model):

    phone = PhoneField()

主/urls.py


from django.urls import path

from . import views


urlpatterns = [

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

]

项目/urls.py


from django.contrib import admin

from django.urls import path,include


urlpatterns = [

    path('admin/', admin.site.urls),

    path('main/',include('main.urls'))

]

视图.py


def insert_my_num(request: HttpRequest):

    phone = Post(request.POST.get('phone'))

    phone.save()

    return redirect('')


守着一只汪
浏览 86回答 1
1回答

慕娘9325324

你views.py有点偏离 - 你没有在任何地方呈现你的表单。我起草了一个快速应用程序(我认为它可以满足您的需求) - 如果它有效,请告诉我:主/模板/index.html在这里,我只是将表单的操作设置为""(这就是您所需要的)并取消注释该form.as_p行<html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; <title>Test Form 1</title></head><body>&nbsp; <form action="" method="post" autocomplete="off">&nbsp; &nbsp; {% csrf_token %}&nbsp; &nbsp; {{ form.as_p }}&nbsp; &nbsp; <input type="submit" value="Send message">&nbsp; </form></body></html>主/views.py请注意这里的差异,我们正在测试请求类型并根据传入的请求类型采取适当的措施。如果是 POST 请求,我们将处理表单数据并保存到数据库中。如果没有,我们需要显示一个空白表格供用户填写。from django.shortcuts import render, redirectfrom .forms import HomeFormdef insert_my_num(request):&nbsp; &nbsp; # Check if this is a POST request&nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; # Create an instance of HomeForm and populate with the request data&nbsp; &nbsp; &nbsp; &nbsp; form = HomeForm(request.POST)&nbsp; &nbsp; &nbsp; &nbsp; # Check if it is valid&nbsp; &nbsp; &nbsp; &nbsp; if form.is_valid():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Process the form data - here we're just saving to the database&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Redirect back to the same view (normally you'd redirect to a success page or something)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('insert_my_num')&nbsp; &nbsp; # If this isn't a POST request, create a blank form&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; form = HomeForm()&nbsp; &nbsp; # Render the form&nbsp; &nbsp; return render(request, 'index.html', {'form': form})让我知道这是否有效!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python