Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to add a variable value to a hyperlink
    text
    copied!<p>This is my function: The problem is that I don't know how to add "value" to the "link".</p> <pre><code> function add_subject_to_subjects_list() { var value = $("#id_name").val(); var link = "/subject/create/"+value; show_subjects_list(); $("#btn-create-subject").click(function() { $.post(link, function(data) { show_subjects_list(); alert(data).hide("fast"); }); }); } </code></pre> <p>Because of that my function post data to the different link: <code>http://127.0.0.1:8000/subject/create/</code> </p> <p>But I want my link depends on value: and it should look like:</p> <pre><code>http://127.0.0.1:8000/subject/create/some_value/ </code></pre> <p>that's the whole js for that page:</p> <pre><code>$(document).ready(function (){ add_subject_to_subjects_list(); }); function show_subjects_list() { $.post("/subject/list/", function(data){ for( var i=0; i&lt;data.length; i++) { $("#list").append('&lt;li&gt;'+data[i]['fields']['name']+'&lt;/li&gt;&lt;br&gt;'); }; }, 'json'); } function add_subject_to_subjects_list() { var value = $("#id_name").val(); var link = "/subject/create/"+value; show_subjects_list(); $("#btn-create-subject").click(function() { $.post(link, function(data) { show_subjects_list(); alert(data).hide("fast"); }); }); } </code></pre> <p>that's my server-side (I'm using Django/Python):</p> <pre><code>@csrf_exempt def subjects_list(request): user = request.user subjects = Subjects.objects.filter(user__exact = user) result = serializers.serialize("json", subjects, fields=('name')) return HttpResponse(result) @csrf_exempt def new_subject(request, subject): subject, created= Subjects.objects.get_or_create( name=subject, user=request.user, created_by=request.user) if created: message = "Subject was created" else: message = "No subject was added to the database" return HttpResponse(message) </code></pre> <p>and that's my html:</p> <pre><code>{% block left-menu %} &lt;div class="sidebar"&gt; &lt;h3&gt;Subjects&lt;/h3&gt; &lt;p&gt; Enter subject name to add &lt;/p&gt; &lt;br&gt; &lt;div id="create-subject"&gt; &lt;form method="post" action="."&gt; {% csrf_token %} {{ subject_creation_form.as_p }} &lt;input type="button" value="Create Subject" id="btn-create-subject" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;div id="subjects-list"&gt; &lt;a id="append"&gt;myID&lt;/a&gt; &lt;ul id="list"&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; {% endblock %} </code></pre> <p>that's the form for that html:</p> <pre><code>class SubjectCreationForm(forms.Form): name = forms.CharField(label="Subject Name", widget=forms.TextInput(attrs={'size':9})) class Meta: exclude = ('created_by', 'created_time', 'num_of_followers', 'vote') def clean_name(self): name = self.cleaned_data['name'] if len(name)&gt;1: return name else: raise forms.ValidationError("Subject name should be longer") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload