114👍
According to the documentation:
A view function, or view for short, is simply a Python function that
takes a Web request and returns a Web response.Each view function is responsible for returning an HttpResponse
object.
In other words, your view should return a HttpResponse
instance:
from django.http import HttpResponse
def myview(request):
return HttpResponse("return this string")
13👍
If you create a chat-bot or need this response on post request for confirmation – you should add decorator, otherwise Django block post requests.
More info you can find here https://docs.djangoproject.com/en/2.1/ref/csrf/
Also in my case I had to add content_type=”text/plain”.
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponse
@csrf_exempt
def Index(request):
return HttpResponse("Hello World", content_type="text/plain")
- [Django]-Find object in list that has attribute equal to some value (that meets any condition)
- [Django]-Substring in a django template?
- [Django]-How to delete a record in Django models?
6👍
You can’t send directly a string, but you can send a JSON object:
from django.http import JsonResponse
def myview(request):
return JsonResponse({'mystring':"return this string"})
Then process that. With Javascript for example if the page was requested by AJAX:
$.ajax({url: '/myview/', type: 'GET',
data: data,
success: function(data){
console.log(data.mystring);
...
}
})
https://docs.djangoproject.com/en/1.11/ref/request-response/#jsonresponse-objects
- [Django]-ImportError: Failed to import test module:
- [Django]-Dynamically add field to a form
- [Django]-Default filter in Django admin
4👍
we use HttpResponse to render the Data
HttpResponse to render the Text
from django.http import HttpResponse
def Index(request):
return HttpResponse("Hello World")
HttpResponse to render the HTML
from django.http import HttpResponse
def Index(request):
text = """<h1>Hello World</h1>"""
return HttpResponse(text)
- [Django]-Extend base.html problem
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
2👍
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('about/',views.aboutview),
path('',views.homeview),
]
views.py
from django.http import HttpResponse
def aboutview(request):
return HttpResponse("<h1>about page</h1>")
def homeview(request):
return HttpResponse("<h1>home page</h1>")
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-ValueError: Dependency on app with no migrations: customuser
- [Django]-How to update fields in a model without creating a new record in django?
1👍
I need to clear the air should someone bump to this in the feature, like wanting to pass any sort of string.
I hard to do the following, starting with the view:
from django.http import HttpResponse
def myview(request):
text = """<a href="https://www.w3schools.com">Visit W3Schools.com!</a>"""
return HttpResponse(text)
Then urls, btw the endpoint here "submit/myview" can be anything
path('submit/myview', views.myview, name='myview'),
the ajax side of things on the template where you want to render this
<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.responseText;
}
xhttp.open("GET", "myview");
xhttp.send();
}
</script>
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-How can I return HTTP status code 204 from a Django view?
- [Django]-Group by Foreign Key and show related items – Django
0👍
According Django documentation Django uses request and response objects to pass state through the system.
When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.Do as follows
from django.http import HttpResponse
def myview(request):
text="return this string"
return HttpResponse(text)
- [Django]-Django Admin Show Image from Imagefield
- [Django]-How to write django test meant to fail?
- [Django]-How to get the currently logged in user's id in Django?