【问题标题】:Difference between two methods in djangodjango中两种方法的区别
【发布时间】:2020-06-01 02:58:17
【问题描述】:

两种方法有什么区别,哪种方法更适合长期使用?有没有比其他的优势?

添加五线谱:

(第一种方法)

views.py


def add_staff(request):
    return render(request, 'hod_template/add_staff_template.html')

def add_staff_save(request):
    if request.method != 'POST':
        return HttpResponse('Method not allowed')
    else:
        first_name = request.GET.get('first_name')
        last_name = request.GET.get('last_name')
        username = request.GET.get('username')
        email = request.GET.get('email')
        password = request.GET.get('password')
        address = request.GET.get('address')
        try:
            user = CustomUser.objects.create_user(username=username, password=password, email=email, last_name=last_name, first_name=first_name, user_type=2)
            user.staffs.address = address
            user.save() 
            messages.success(request, 'Staff Added Successfully')
            return HttpResponseRedirect('/add_staff')
        except:
            messages.error(request, 'Failed to Add Staff')
            return HttpResponseRedirect('/add_staff')

urls.py

    path('add_staff/', add_staff),
    path('add_staff_save/', add_staff_save),

add_staff.html

    <form role="form" action="/add_staff_save">
        {% csrf_token %}
        <div class="card-body">
            <div class="form-group">
                <label>Email address</label>
                <input type="email" class="form-control" name="email" placeholder="Enter email">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control" placeholder="Password" name="password">
            </div>

            <!-- same for first_name, last_name, username, address --> 
            <div class="card-footer">
                <button type="submit" class="btn btn-primary btn-block">Add Staff</button>
            </div>
        </div>
    </form>

(第二种方法)

在forms.py中创建一个包含first_name、last_name、username、address所有字段的表单 然后调用视图并验证它。 forms.py

    class StaffForm(forms.ModelForm):
        class Meta:
            model = Staff
            fields = ('first_name', 'last_name', 'username', 'address')

views.py

def add_staff(request):
    if request.method == 'POST':
        form = StaffForm(data=request.POST)
        if form.is_valid():
            messages.success(request, 'Staff Added Successfully')
            form.save()
    else: 
        form = StaffForm()
    return render(request, 'staff.html', {'form':form})

urls.py path('add_staff/', add_staff),

staff.html

    <form role="form" action="/">
        {% csrf_token %}
        {{ form.as_p }} <!-- render form as paragraph -->
    </form>

两种方法都运行良好,并且人员模型具有所有必填字段。 很抱歉这个问题太长了,因为我没有添加员工模型。如果你需要请告诉我。

【问题讨论】:

    标签: django django-views


    【解决方案1】:

    当然,第二种方法比第一种方法好得多 因为您应该在每个单独部分中的其他文件(如 forms.py)中进行清理和验证

    您还可以添加更多选项和其他可能在一段时间后有用的东西 你应该意识到这一点并预测一些改进的变化! 并且通过制作表单和使用泛型,您将编写的代码比在视图中的硬编码中自己编写的代码更少。

    所以不要犹豫,选择方法2

    【讨论】:

    • 我可以在第二种方法中使用引导程序吗?
    【解决方案2】:

    第二种方法比第一种方法好。第二种方法提供了更大的灵活性,您只需在 forms.py 中创建一个表单,您可以在不同的位置使用它,而第一种方法您必须使用 2 个 url 来显示表单并保存它。您也可以在表单上使用crispy_forms_tags 之类的过滤器{{ form|crispy }}。所以很明显,从长远来看,第二种方法更好。

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2019-03-31
      相关资源
      最近更新 更多