how to handle the render respose in django - python

first page of my application : home.html
i have written the view to render the request to "localhost:8080/contact"
def contact(request):
..
return render(request,'template/contact.html',{'contact': [ htmlString ] })
Now, localhost:8080/contact is working correctly and showing the output. but i need to include the same output in home.html
i tried to add <a href ="template/contact.html></a> in home.html but it is now showing the results.
Can someone explain how to do this..
I am beginner in python django development.

Related

(Django) Why would my code not execute in an .html template?

this is my first post. I'll try get things right first time. I am having a play around with django and slowly creating a dummy store website. I'm following along a Corey Schafer YouTube tutorial and I didn't have this issue on my first run, so scratching my head now!
My problem is that my .html template is not displaying the listing item when I run the page. I still see the <h1>DaftPrices Store Homepage</h1>, but not the listings.
This is my template (store_home.html):
<!DOCTYPE html>
<html lang="en">
<head>
<title>DaftPrices - Home</title>
</head>
<body>
<h1>DaftPrices Store Homepage</h1>
{% for listing in listings %}
<p>{{ listing.item }}</p>
{% endfor %}
</body>
</html>
This is saved in a store dir, inside a templates dir, inside the store app.
Image of directories
This is my app views.py:
from django.shortcuts import render
listings = [
{
'seller': 'ABCDEFG',
'title': 'Something to sell',
'date_posted': '28th August 3030'
}
]
def store_home(request):
context = {'listing': listings}
return render(request, 'store/store_home.html', context)
def store_about(request):
return render(request, 'store/store_about.html')
To clarify, the page pulls the template, but the "code" isn't working
I have tried re-writing the dummy listings and changing the variables. I wasn't expecting this to do anything (and it didn't), but that was my first shot.
I checked my project's settings.py folder to double check that I had added the store app into "installed apps". I believe this is correct, and was identical to what I did on my previous run without the issue:
store app ; apps.py:
from django.apps import AppConfig
class StoreConfig(AppConfig):
name = 'store'
project ; settings.py:
INSTALLED_APPS = [
'store.apps.StoreConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I've had a look at other stackoverflow listings which seem like they are relevant to my problem, but I'm struggling to identify the key info other than making sure the context is called for correctly. I can't see any issues with mine, hence I'm here.
You have missed the s in listings context key:
def store_home(request):
context = {'listing': listings}
return render(request, 'store/store_home.html', context)
def store_home(request):
context = {'listings': listings}
return render(request, 'store/store_home.html', context)
Corey did awesome job. I have completed his course and I work with Django today.

I can't seem to link to html pages in a website using Django

I've been trying to develop a website using Django, I can open the homepage but whenever I click on a link that's supposed to direct me to another page, it won't work
def homepage(request):
return render(request,'AlzaidStudApp/homepage.html')
I'm not sure if I should add a new function for the rest of the pages or not
path('', views.homepage, name='home-page'),
The link in the HTML page is
<li>gallery</li>
I made a view function for the gallery page with
def gallery(request):
return render(request,'AlzaidStudApp/gallery.html' )
I also added a URL path:
path('gallery', views.gallery, name='gallery'),
The error I'm getting is:
Using the URLconf defined in AlzaidStud.URLs, Django tried these URL patterns, in this order:
admin/
gallery [name='gallery']
[name='home-page']

Django Template Language Not Rendering

I am brand new to Django and following along with the tutorial. I'm hoping this is just an obvious mistake, but I am unable to get my web browser to render anything written in the Django template language and I can't figure out why.
Here is my directory structure for some context: https://imgur.com/dGNIiDa
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('budget/', include('budget.urls')),
path('admin/', admin.site.urls)
]
budget/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('<int:account_id>/', views.get_account, name='detail'),
]
budget/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from budget.models import Account, Transaction
def get_account(request, account_id):
accts = Account.objects.filter(pk=account_id)
context = {"test": accts}
return render(request, 'budget/detail.html', context)
budget/templates/budget/detail.html:
<p>This is a {{ context.test }}</p>
When I visit localhost:8000/budget/1 in my browser this is all that is rendered: https://imgur.com/j2Vh0yb
Clearly Django is finding the template file and sending it to the browser, but anything that is written inside of {} does not get recognized or rendered at all. I've followed along exactly with the tutorial and I have no idea why it's not working. Any ideas?
You don't need context in the expression in the template; all that you put in the context are "globals" in the template, so try
<p>This is a {{ test }}</p>
instead.
Django's template engine has the unfortunate property of being silent about nonexisting properties, so it's hard to debug stuff like this.

Incorporating existing html pages into Django

I have several hundred static html pages, with an index html page, that I need to bring into Django. I can't figure out the easiest way to do that, I know I'm missing something simple. Any advice?
Nothing fancy needed, just need to dump them in a directory and allow users to navigate to them.
You need to create a view and a url for each html template, iI'm going to put you a simple example here but it's highly recommended that you read Django's documentation or a tutorial :
First, you create a view in a views.py file :
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.generic import View
class LoadTemplateView(View):
template_name = ['thenameofyourdjangoapp/yourtemplatename.html']
#You put any code you may need here
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
Then, you must create a url that reads that view in a urls.py file :
from django.conf.urls import patterns, url
#Here you import from views the view you created
from .views import LoadTemplateView
urlpatterns = patterns(
url(r'^myurl/$', LoadTemplateView.as_view(), name="load_template"),
)
Last, in your let's say home html template, you assign this url to a submit button in order to call it by the name you gave it on urls.py (load_template in this case) :
<html>
<body>
<div>
<a class="option-admin" id="id_go" href ="{% url 'yourdjangoappname:load_template' %}"> Go to template </a>
</body>
</html>
</div>
As I said anyway, it's better that you read the complete Django documentation as well:
https://docs.djangoproject.com/en/1.9/
If these are legacy static pages that you do not plan to touch again, then I'd leave django out of it. I'd put them all in a directory or subdomain and serve them directly from the server (probably nginx, maybe apache, whatever you're using). As a general rule, you don't want Django serving static assets, you want the proxy server serving them.
You COULD move them into Django and manage them like other Django static assets, as described in the Managing Static Files Documentation, but if they're already out there, then there's not a whole lot of advantage over just serving them as outlined above.
And finally, if you wish to fully integrate them into your Django site, then you should probably start with the template documentation.

href attribute value not working in Python Flask app for target HTML pages

I have a basic flask application with the following structure:
app.py
templates/index.html
static/js/common.js
static/css/common.css
In the top navigation bar of my index.html file, I want to have menu buttons contain links to other HTML pages. However, when I tried various ways of specifying the target html files in href attribute of a tags, it doesn't work. On click, the page fails to load.
What is the correct and quick way of mentioning target HTML files/path in my index.html?
<body>
<div class="navbar-header">
<strong>My Website</strong>
</div>
</body>
The template (index.html) needs to be rendered by a route in your Flask application.
To render the index.html template with the URL /index.html:
#app.route('/index.html')
def index():
return render_template('index.html')
Refer to this section in the quickstart guide:
http://flask.pocoo.org/docs/0.10/quickstart/#rendering-templates
It's probably worth reading through the whole guide at the same time. As a side note, it's unconventional to include .html extensions in your URLs in Flask, but it's your call.

Categories

Resources