I am fetching the current url of the browser (which keeps changing) in a javascript variable (say var_url). I have a django model Url. I want to create its object using a view function create_url().
Earlier When I used a form, I used request.POST['field']. Now I don't have a form and I want to pass the value of var_url. How can that be done?
This was my view function when I was using a form:
def create_url(request):
if request.method == 'POST':
text=request.POST['field']
url_result=Url.objects.get_or_create(
url_text=text
)
return HttpResponse('Url Object Created')
And the following was my form:
<form name='urlForm' method="post" action="{% url 'create_url'%}" id="create" >
{% csrf_token %}
<input type="text" id="myurl" name="field"/>
<input type="submit" name="submit" value="Submit">
</form>
Now rather than a form, I have this:
<div id='results'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
window.addEventListener('message',function(e){
results.innerHTML = e.data;
});
</script>
I want to send the content of div tag to my view function.