Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:02:00+00:00 2026-05-25T19:02:00+00:00

I have a django template which also has a div element that takes in

  • 0

I have a django template which also has a div element that takes in a user_inputed value.
When the value is entered, I call a javascript function say onSubmit(user_input)

<input type="text" class= "inputtext" onKeyPress="return onSubmit(this.value)">

Now in this onSubmit() function which now has the user-inputted value user_input, I want to be able to use url patterns to a direct to a view, like

function onSubmit(user_input) {window.location = "{% url myview user_input %}";}

The problem here is that since user_input is empty when the template is loaded, the url-view reverse lookup gives an error. Is there a way to trigger this lookup only when the onSubmit function is called.

I know form is an alternative, but it just feels like it’ll be an overkill for this situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:02:01+00:00Added an answer on May 25, 2026 at 7:02 pm

    You can get the URL via AJAX:

    views.py:

    def get_url(request):
        name = request.GET.get('name')
        args = reguest.GET.get('args', [])
        kwargs = request.GET.get('kwargs', {})
        try:
            url = django.core.urlresolvers.reverse(name, args=args, kwargs=kwargs)
        except NoReverseMatch:
            url = None
        return django.http.HttpResponse(url)
    

    urls.py

    #...
    ('^url$', get_url)
    #...
    

    js:

    function onSubmit(user_input) {
        var args = [user_input];
        jQuery.get('/url', {'args': args}, function(data) {
            var url = data;
            if (url) {
                window.location = url;
            } else {
                alert('fail');
            }
        });
    }
    

    Alternatively, if your URL rule is simple enough, you can use some placeholder when resolving and URL, and before submitting the form you should replace it with real input:

    var fakeUrl = '{% url myview "%s%" %}';
    function onSubmit(user_input) {
        window.location = fakeUrl.replace('%s%', user_input);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django template which extends the base template that has code to
I have a django app which has html templates and I also have a
I have a Django template that I want to extend in multiple places. In
So, I have a django project that is using jinja2 rendering, and I also
I have a Django project which has two apps (one created as debug test).
I have a django template which I have to send as email body, the
In one of my Django templates, I have a chunk of HTML which repeats
Does Django have any template tags to generate common HTML markup? For example, I
I have a basic question, in the Django template language how can you tell
I have some french letters (é, è, à...) in a django template but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.