Search code examples
pythondjangodjango-apps

Django project - two app's two views in one url how to solve


In my django project i have created one app for log-in users. And another app for sign-up user. But in my .html template both sign-up and log-in function are given on the same page means in one url with one .html file . Show basically what it means i have to use two apps two different views in one url as well as one .html file . How is it possible
Cannot combine them in one app cause both of the app has lots of other related functionalities also. Should i make different app according to the html page/ or one url one app method ?


Solution

  • You can not have two views for one URL.

    But you can make a view and link the URL to it and in that view decide which function should be used.

    If you have both login and signup form in one page then you can use 2 different forms and then decide which function should be used.

    This is my solution for you:

    1 - make a single view for your URL

    2 - make 2 forms, one for signup and one for login.

    3 - in your template add name to your submit buttons and give them a value based on their form:

    # login form
    <form>
        # form fields
        <button type="submit" name="button-name" value="login">
    </form>
    
    # signup form
    <form>
        # form fields
        <button type="submit" name="button-name" value="signup">
    </form>
    

    now in your view you can decide which function should be used:

    if request.POST.get('button-name') and request.POST.get('button-name') == "signup":
        # signup the user
    elif request.POST.get('button-name') and request.POST.get('button-name') == "login":
        # login the user
    

    i don't recommend you to call another view in this view because views should render a template or ... but i recommend you to make 2 different functions for login and signup which they can be anywhere and call them and decide what you want to do based on what they will return.

    Or you can just do everything here.