Django admin custom form doesn't reflect in server - python

In django admin I need to upload multiple file. So customise change_form.html. This changes working nice in my local environment. But do not work in server.
in change_form.html
{% extends "admin/change_form.html" %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
<fieldset class="module">
<h2>Upload Photos</h2>
<input name="bundle_images" type="file" multiple />
</fieldset>
{% endblock %}
in admin.py
class ImageBundleAdmin(admin.ModelAdmin):
change_form = 'templates/admin/imagebundles/ImageBundle/change_form.html'
.....
.....
The output in my local
But output in my server
Please advice what I miss? In server I used nginx and gunicorn
Thanks

Related

jinja2 template expressions

Hy, i am very new to web dev and jinja2. i encountered a problem with jinja, when i render template and want to output user name to web page who loged in...well i cant get one. Here is my index.html file with jinja expression in tag:
{% extends "base.html" %}
{% block content %}
{% if user %}
<h1>Welcome to Flask authentication site, {{ user.name }}!</h1>
{% else %}
<h2>Hello, log in or sign up</h2>
{% endif %}
{% endblock content %}

Error adding a convential sign up and github sign up together in a django project

I am doing a django book store project which has a successful sign up function. I followed WS Vincent's tutorial to connect Github signup (https://learndjango.com/tutorials/django-allauth-tutorial) and I have come across an error when integrating it.
TemplateSyntaxError at /
Invalid block tag on line 16: 'provider_login_url', expected 'endif'. Did you forget to register or load this tag?
Below is the code for the major areas of my project.
home.html
{% extends '_base.html' %}
{% load static %}
{% block title %}Home{% endblock title %}
{% block content %}
<h1>Homepage</h1>
<img class="bookcover" src="{% static 'images/djangoforprofessionals.jpg' %}">
{% if user.is_authenticated %}
<p>Hi {{ user.email }}!</p>
<p>Log Out</p>
{% else %}
<p>You are not logged in</p>
<p>Log In |
Sign Up
Sign Up</p>
{% endif %}
{% endblock content %}
I am not familiar with HTML but I take the two sign up lines are not meant to be put one after the other.
According to the documentation, it seems like you forget to load {% load socialaccount %}.
Reference

Flash messages with flask-bootstrap and flask-nav

I'm using flask-bootstrap to use the Bootstrap front-end for a flask web application. Unfortunately, since I've started using flask-bootstrap and flask-nav, I'm not able to display flash messages.
This is the base.html:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
{{ nav.mynavbar.render() }}
{% endblock %}
<html>
<body>
<hr>
<div class='container'>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
</html>
This is one of the view that should flash a message when a change is saved:
#app.route('/model/<model_name>/edit', methods=['GET', 'POST'])
def edit(model_name):
"""Edit model's configuration.
Args:
model_name [str]: The name of the model we want to change the
configuration.
"""
# return to index page if model is not found in the database
model = DSModel.query.filter_by(name=model_name).first()
if not model:
flash('Model {} not found.'.format(model_name))
return redirect(url_for('index'))
# pre-load current model's configuration
form = ConfigurationForm()
# if it's a POST request, it means the user is trying to change the model's
# configuration. Save changes in the database
if request.method == 'POST': # and form.validate_on_submit():
model.configuration.configuration = form.value.data
model.configuration.datetime = datetime.datetime.utcnow()
db.session.add(model)
db.session.commit()
flash('Your changes have been saved.', 'success')
return redirect(url_for('edit', model_name=model_name))
else:
form.value.data = model.configuration.configuration
return render_template('edit.html', form=form)
Finally, this is the __init__.py:
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from flask_sqlalchemy import SQLAlchemy
nav = Nav()
#nav.navigation()
def mynavbar():
return Navbar(
'Dashboard',
View('Home', 'index'),
View('Login', 'login')
)
app = Flask(__name__)
Bootstrap(app)
nav.init_app(app)
app.config.from_object('config')
db = SQLAlchemy(app)
from app import views, models
I think there must be something funky with my base.html file, but I'm not terribly familiar with HTML, so I'm not able to find what's wrong. I've looked into examples online (i.e. here), but the format seems to be pretty similar to what I'm doing.
EDIT: This is the edit.html file:
{% extends 'base.html' %}
{% block content %}
<h1>Update configuration</h1>
<form action='' method='post' name='configuration'>
<p>
Please update the current model configuration:<br>
{{ form.value(cols='150', rows='20') }}
<p><input type='submit' value='Save'></p>
</form>
{% endblock %}
Try to edit your base.html to this:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
{{ nav.mynavbar.render() }}
{% endblock %}
<html>
<body>
<hr>
<div class='container'>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div>
{% block content %}{% endblock %}
</div>
</body>
</html>

app_index.html => app is in adminsite to see

i have a small problem... i want to bind my app to adminsite. my app doesnot use models.py so the only way to make my app visible in adminsite is to override the app_index.html from django's admin/templates directory. my problem is that i dont know how to override this sothat my app is in admin. i v read all the docs, but the exact way how to do this is hard to find...
here is that app_index.html:
{% extends "admin/index.html" %}
{% load i18n %}
{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs"><a href=".../">
{% trans "HOME" %}</a>
{% for app in app_list %}
{% blocktrans with app.name as name %} {{ name }} {% endblocktrans %}
{% endfor %}</div>{% endblock %}
{% endif %}
{% block sidebar %}{% endblock %}
thanks in advance...
best regards,
you don't need models.py, just register a "Model" with the admin site.
admin.site.register(class MyMenuThing(models.Model): pass)

Django doesn't seem to detect my login.html, block problem?

I'm creating a web app with django 1.2.4.
I am using contrib.auth.views.login, I have followed every step but it seems I have forgotten something cause I don't see the login form. Here is my folder structure:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...and here is my login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...and in my base.html I have:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
I have a basestyle.css included in base.html and the other templates inherit correctly too... it seems to be a block problem...
Any solution??
Thnak you
Instead of inserting of a block I used the include tag in base.html, just like this:
{% include "registration/login.html" %}
If you’d prefer not to call default (django provided) template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf.
For example, this URLconf line would use myapp/login.html instead:
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),
Reference : Django official documentation
It solves my problem. Hope this works for others.

Categories