Пример #1
0
def restaurant_sign_up(request):
    user_form = UserForm()
    restaurant_form = RestaurantForm()

    if request.method == 'POST':
       user_form = UserForm(request.POST)
       restaurant_form = RestaurantForm(request.POST, request.FILES)

       if user_form.is_valid() and restaurant_form.is_valid():
           new_user = User.objects.create_user(**user_form.cleaned_data)
           new_restaurant = restaurant_form.save(commit=False)
           new_restaurant.user = new_user
           new_restaurant.save()

           login(request, authenticate(
                username = user_form.cleaned_data["username"],
                password = user_form.cleaned_data["password"]
           ))

           return redirect(restaurant_home)

    return render(request, 'restaurant/sign_up.html', {
         "user_form": user_form,
         "restaurant_form": restaurant_form
    })
Пример #2
0
def restaurant_sign_up(request):
    user_form = UserForm()
    restaurant_form = RestaurantForm()

    if request.method == "POST":
        user_form = UserForm(request.POST)
        restaurant_form = RestaurantForm(request.POST, request.FILES)

        if user_form.is_valid() and restaurant_form.is_valid():
            new_user = User.objects.create_user(**user_form.cleaned_data)
            new_restaurant = restaurant_form.save(commit=False)
            new_restaurant.user = new_user
            new_restaurant.save()

            login(request, authenticate(
                username = user_form.cleaned_data["username"],
                password = user_form.cleaned_data["password"]
            ))

            return redirect(restaurant_home)

    return render(request, "restaurant/sign_up.html", {
        "user_form": user_form,
        "restaurant_form": restaurant_form
    })
Пример #3
0
def restaurant_sign_up(request):
    user_form = UserForm()
    restaurant_form = RestaurantForm()

    #Check if user request submit button
    #Get data from UserForm and RestaurantForm
    if request.method == "POST":
        user_form = UserForm(request.POST)
        restaurant_form = RestaurantForm(request.POST, request.FILES)

        if user_form.is_valid() and restaurant_form.is_valid():
            #Create new user object and check validation
            new_user = User.objects.create_user(**user_form.cleaned_data)
            #Create new restaurant object in mem and do not submit data
            new_restaurant = restaurant_form.save(commit=False)
            new_restaurant.user = new_user
            new_restaurant.save()

            login(
                request,
                authenticate(username=user_form.cleaned_data["username"],
                             password=user_form.cleaned_data["password"]))

            return redirect(restaurant_home)

    return render(request, 'restaurant/sign_up.html', {
        "user_form": user_form,
        "restaurant_form": restaurant_form
    })
Пример #4
0
def restaurant_sign_up(request):
    user_form = UserForm()
    restaurant_form = RestaurantForm()

    #after user click on submit data
    if request.method == "POST":
        #get data from userform and restaurant form
        user_form = UserForm(request.POST)
        restaurant_form = RestaurantForm(request.POST, request.FILES)

        #check if data is valid
        if user_form.is_valid() and restaurant_form.is_valid():
            #create a new user object (restaurant owner)
            new_user = User.objects.create_user(**user_form.cleaned_data)
            #create a new restaurant object. commit false means create in memeory first
            new_restaurant = restaurant_form.save(commit=False)
            #assign user (restaurant owner) to restaurant
            new_restaurant.user = new_user
            #now save it
            new_restaurant.save()

            login(
                request,
                authenticate(username=user_form.cleaned_data["username"],
                             password=user_form.cleaned_data["password"]))
            #go back to restaurant home page
            return redirect(restaurant_home)

    return render(request, 'restaurant/sign_up.html', {
        "user_form": user_form,
        "restaurant_form": restaurant_form
    })
Пример #5
0
def restaurant_sign_up(request):
    user_form = UserForm()
    restaurant_form = RestaurantForm()

    #when sign up button is clicked, this block is executed
    if request.method == "POST":
        user_form = UserForm(request.POST)  #getting data from user form
        restaurant_form = RestaurantForm(
            request.POST, request.FILES)  #getting data from restaurant form

        if user_form.is_valid() and restaurant_form.is_valid(
        ):  #check the data received from the forms before proceeding
            new_user = User.objects.create_user(
                **user_form.cleaned_data
            )  #creates a new User object "restaurant owner"
            new_restaurant = restaurant_form.save(
                commit=False
            )  #creates a new Restaurant BUT **'commit' to memory only**
            new_restaurant.user = new_user  #attach the User of the restaurant object
            new_restaurant.save()  #save Restaurant object - this saves to DB

            #Then login user
            login(
                request,
                authenticate(
                    username=user_form.cleaned_data["username"],
                    password=user_form.cleaned_data["password"],
                ))

            #Then redirect to 'restaurant_home' page
            return redirect(restaurant_home)

    return render(request, 'restaurant/sign_up.html', {
        "user_form": user_form,
        "restaurant_form": restaurant_form
    })