Как получить JSON Ответ на регистрационную форму в Django - PullRequest
0 голосов
/ 07 января 2020

Я использую веб-сайт, используя Django, и я новичок в этом. Вот мой код:

views.py:

def signup(request):
        registered = False
        failed_ref = False
        wrong_ref = False
        user_err = ''
        mobile_err = ''
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = ProfileForm(data=request.POST)
        if 'city' in request.POST:
            if user_form.is_valid() and profile_form.is_valid():
                user = user_form.save()
                user.set_password(user.password)
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                if 'image' in request.FILES:
                    profile.image = request.FILES['image']
                print(request.POST)
                profile.save()
                try:
                    ref_con = profile.referral_contact
                    if ref_con == profile.mobile_no:
                        failed_ref = True
                    elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
                        prof = Profile.objects.get(mobile_no=ref_con)
                        wallet_rec = Wallet.objects.get(profile=prof)
                        wall = Wallet.objects.get(profile=profile)
                        registered = True
                except Profile.DoesNotExist:
                    wrong_ref = True
                data = {'registered': registered, 'failed_ref': failed_ref, 'wrong_ref': wrong_ref}
                return JsonResponse(data)
            else:
                print(user_form.errors, profile_form.errors)
                user_err = ''
                mobile_err = ''
                if user_form.errors:
                    user_err = "A profile with this username already exists!"
                if profile_form.errors:
                    mobile_err = "A profile with this mobile number already exists!"
                data = {'registered': registered, 'failed_ref': failed_ref, 'wrong_ref': wrong_ref,
                        'user_error': user_err,
                        'profile_error': mobile_err}
                return JsonResponse(data)
    else:
        user_form = UserForm()
        profile_form = ProfileForm()
    return render(request, 'accounts/profile_form.html',
                  {'user_form': user_form, 'profile_form': profile_form, 'registered': registered,
                   'failed_ref': failed_ref, 'wrong_ref': wrong_ref})

Мой вопрос заключается в том, как получить ответ в JSON для этой функции. Я пытался, но не смог получить ответ в JSON. Любые предложения?

Вот ProfileForm ():

class ProfileForm(forms.ModelForm):

    class Meta:
        model=Profile
        widgets = {
        'address_line_1': forms.TextInput(attrs={'placeholder': 'Door No,Building'}),
        'address_line_2': forms.TextInput(attrs={'placeholder': 'Area,Locality'}),
    }
        fields=('first_name','last_name','mobile_no','email','address_line_1','address_line_2','postal_code','city','country','image','referral_contact','promo_coupon','ic')

Ответы [ 2 ]

0 голосов
/ 07 января 2020

Попробуйте использовать django сериализаторы

Ссылка: https://docs.djangoproject.com/en/3.0/topics/serialization/

в этом JSON

0 голосов
/ 07 января 2020

Вот что я думаю

def signup(request):
    if request.method == 'POST':
        # save data in database and send response accordingly
    else:   # GET request
        # render empty signup page 
        # why would you need any params in empty signup page?
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...