【问题标题】:How do I migrate vieys.py file without exceptions?如何无例外地迁移 vieys.py 文件?
【发布时间】:2022-01-17 00:24:29
【问题描述】:

当我尝试迁移我的 .py 文件时,我遇到了一个错误“^ if request.method == 'POST' SYNTAX ERROR : Invalid character in Identifier error”

这是我的代码

def register(request):
    registered = False
    if request.method() == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        # Check to see both forms are valid
        if user_form.is_valid() and profile_form.is_valid():
            # Save User Form to Database
            user = user_form.save()
            # Hash the password
            user.set_password(user.password)
            # Update with Hashed password
            user.save()
            # Now we deal with the extra in
            # Can't commit yet because we still need to manipulate
            profile = profile_form.save(commit=False)
            # Set One to One relationship between
            # UserForm and UserProfileInfoForm
            profile.user = user
            # Check if they provided a profile picture
            if 'profile_pic' in request.FILES:
                print('found it')
                # If yes, then grab it from the POST form reply
                profile.profile_pic = request.FILES['profile_pic']
                # Now save model
            profile.save()
            # Registration Successful!
            registered = True
        else:
            # One of the forms was invalid if this else gets called.
            print(user_form.errors,profile_form.errors)
    else:
        # Was not an HTTP post so we just render the forms as blank.
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    # This is the render and context dictionary to feed
    # back to the registration.html file page.
    return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered})

请帮帮我,谢谢!!

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    改变你的错误 =

    if request.method() == 'POST':
    

    到 =>

    if request.method == 'POST':
    

    【讨论】:

    • 谢谢..它工作..以前我没有使用括号。但它仍然有效..
    猜你喜欢
    • 2011-11-26
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2018-03-09
    相关资源
    最近更新 更多