To retreive Mysql Database, and add this to news page. Here are some tips.
Retreive Data From MySQL Database
- In views.py file, create a new object and use
obj = News.objects.get(id=1)
to get information where id = 1.
def NewsP(request):
# return HttpResponse('<h1>This is our latest news</h1>')
obj = News.objects.get(id=1)
context = {
"data": obj
}
return render(request, 'news.html', context)
- And in news.html, you can access these data in following way.
{% extends 'base.html' %}
{% block title %} News {% endblock %}
{% block body %}
<h1>This is our news page content</h1>
<!-- <p>This is our content description</p> -->
<h1>{{ data.author }}</h1>
<h2>{{ data.title }}</h2>
<p>{{ data.description }}</p>
{% endblock %}
-
Refresh your launched page, you will see this as a result.