Render works but url does not change

499 views
Skip to first unread message

Derrick Lu

unread,
Nov 15, 2018, 9:36:03 PM11/15/18
to Django users
HI,
I am having trouble with url when rendering to a new page via a button.
The new page renders but the url does not reflect the new page.
Has anyone had that problem before?

Joel

unread,
Nov 16, 2018, 12:52:14 AM11/16/18
to django...@googlegroups.com
Post details.. The url, your urls.py etc

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a745765-e3a8-4929-baae-b51ed922ae2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Derrick Lu

unread,
Nov 16, 2018, 1:56:56 AM11/16/18
to Django users
This is my url.py

app_name = 'springcity'

urlpatterns = [
        url(r'^$', views.index, name='index'),
        url(r'^home/$', views.home, name='home'),
        url(r'^portal/$', views.portal, name='portal'),
        url(r'^ajax/cutlist/$', views.cutList, name='cutlist'),
]

This is my views.py

def home(request):
    if request.method == "POST":
        if 'Search' in request.POST:
            modelIn = request.POST['mid']
            db = get_object_or_404(PDetails, model=modelIn)
            form = calcForm()
            form.fields["stc"].initial = db.stc
            form.fields["ocv"].initial = db.ocv
            form.fields["mppv"].initial = db.mppv
            form.fields["nvtc"].initial = db.nvtc
            form.fields["mmppc"].initial = db.mmppc
            form.fields["maxov"].initial = db.maxov
            form.fields["maxc"].initial = db.maxc
            args = {
                'calcForm': form,
            }
            return render(request, 'calculator/portal.html',args)

        else:
            if 'MInput' in request.POST:
                form = calcForm()
                args = {
                    'calcForm': form,
                    }
                request.method = "GET"
                return render(request, 'calculator/portal.html',args)      
    else:
        if request.method == "GET":
            objectlist = Brand.objects.all().order_by('brand')
            args = {
                'objectlist': objectlist,
                }
            return render(request, 'calculator/home.html',args)

Then this is my html that I start off with.

{% extends 'calculator/index.html' %}

{% block title %} Home {% endblock %}

{% block link %}
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'calculator/css/home.css' %}"/>
    <script type="text/javascript" src="{% static 'calculator/js/home.js' %}"></script>
{% endblock %}

{% block main %}
    <form action="{% url 'springcity:home' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}

        <div class="row center">
            <div class="input-field col s3 offset-s4">
                <select id="brandID">
                    <option value="">Choose Company</option>
                    {% for brand in objectlist %}
                        <option value='{{brand.brand}}'>{{ brand.brand }}</option>
                    {% endfor %}
                </select>
                <!--<label>Choose Company</label>-->
            </div>
        </div>

        <div class="row center">
            <div id="modelRow" class="input-field col s3 offset-s4" hidden>
                <select name="mid" id="modelID">
                    <option value="">Choose Model</option>
                </select>
                <!--<label>Choose Model</label>-->
            </div>
        </div>

        <div class="row center">
            <div class="col s3 offset-s4">
                <div class="button-center">
                    <button type="submit" class="btn waves-effect waves-light" name="Search" id="search">Search</button>
                </div>
            </div>
        </div>

        <div class="row center">
            <div class="col s3 offset-s4">
                <div class="button-center">
                    <button type="submit" class="btn waves-effect waves-light" name="MInput" id="manual_input">Manual Input</button>
                </div>
            </div>
        </div>
    </form>
{% endblock %}

Jason

unread,
Nov 16, 2018, 7:50:09 AM11/16/18
to Django users
You're rendering templates and returning the response, but if you want the URL to change, you need to do a redirect.

Right now, you're telling django for button submits, render the portal template and return the html.  There's nothing in it about redirecting to another URL
Reply all
Reply to author
Forward
0 new messages