Home > Software design >  How do I redirect to the created page after I submitted the form Django
How do I redirect to the created page after I submitted the form Django

Time:11-30

I'm trying to redirect to the created page after I've filled out and submitted a form. I have gotten it to work on the update form but not the create form. How do i do this?

Here's what I have so far. Let me know if you need more details and code

views.py

@login_required(login_url='login')
def createRoom(request):
    form = RoomForm()
    topics = Topic.objects.all()
    if request.method == 'POST':
        topic_name = request.POST.get('topic')
        topic, created = Topic.objects.get_or_create(name=topic_name)

        Room.objects.create(
            host=request.user,
            topic=topic,
            name=request.POST.get('name'),
            assigned=request.user,
            status=request.POST.get('status'),
            priority=request.POST.get('priority'),
            type=request.POST.get('type'),
            description=request.POST.get('description'),
        )
        return render('room', pk=room.id)

    context = {'form': form, 'topics': topics, 'room': room}
    return render(request, 'room/room_form.html', context)

But this throws this error

traceback

Traceback (most recent call last):
  File "C:\Users\mikha\issue_env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\mikha\issue_env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\mikha\issuetracker\base\views.py", line 68, in createRoom
    return render('room', pk=room.id)

Exception Type: AttributeError at /create-room/
Exception Value: 'function' object has no attribute 'id'

CodePudding user response:

While you have created a new Room object, you haven't assigned it to room.

Try

room = Room.objects.create(

CodePudding user response:

Your create room function should look like this

@login_required(login_url='login')
def createRoom(request):
    form = RoomForm()
    topics = Topic.objects.all()
    if request.method == 'POST':
        topic_name = request.POST.get('topic')
        topic, created = Topic.objects.get_or_create(name=topic_name)

        room = Room.objects.create(
            host=request.user,
            topic=topic,
            name=request.POST.get('name'),
            assigned=request.user,
            status=request.POST.get('status'),
            priority=request.POST.get('priority'),
            type=request.POST.get('type'),
            description=request.POST.get('description'),
        )
        room.save() 
        return redirect("created-room-view-function")

    context = {'form': form, 'topics': topics, 'room': room}
    return render(request, 'room/room_form.html', context)
  • Related