Django form doesn't show available values - python

I'm quite new to Django...
So, I have a model. Notice the two foreign key fields (DeviceProfile model actually has data, but MISMStateSnapshot does not):
class MISMWorkflow(models.Model):
createdAt = models.DateTimeField(default=timezone.now)
currentSnapshot = models.ForeignKey('MISMStateSnapshot', null=True, blank=True, on_delete=models.SET_NULL)
device = models.ForeignKey(DeviceProfile, null=True, blank=True, on_delete=models.SET_NULL, related_name='workflows')
def get_absolute_url(self):
return reverse('{}:workflow_detail'.format(VIEW_NAMESPACE), args=(self.pk,))
And a CreateView:
class WorkflowCreateView(generic.CreateView):
model = MISMWorkflow
fields = '__all__'
template_name = 'mism_web/workflow_create_form.html'
def form_valid(self, form):
form.instance.device = DeviceProfile.objects.get(pk=self.kwargs.get('device_id'))
form.instance.createdAt = timezone.now()
return super(WorkflowCreateView, self).form_valid(form)
And the template:
{% extends 'mism_web/base.html' %}
{% load material_form %}
{% block content %}
<form action="" method="POST">{% csrf_token %}
<!--{{ form.as_p }}-->
{% form form=form %}{% endform %}
<input type="submit" name="_submit" class="btn" value="Save" />
</form>
{% endblock %}
urls:
url(r'^workflow/workflow_create/$', workflow.WorkflowCreateView.as_view(), name='workflow_create_new'),
This is what I see when I go to create page:
There are couple of things wrong with this:
I don't see the calendar/clock widget for the DateTimeField (createdAt) that I expected to see here.
There's no selection input for either currentSnapshot or device fields (Even though there are devices in the DB)
This is not because I am using the django-material plugin. I tested without it and I still get the same "empty" form.
What's the cause of this and how to fix?
EDIT:
This is what I see in the admin panel for `MISMWorkflow', this is exactly the sort of form I expect to see (ofc, I don't want to create foreign key entities from here, just to list them down):
EDIT: The problem seems to happen only when I extend the base template. If I don't use it, I can see the fields properly. For example, template will be like so:
<html>
<body>
<form action="" method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="_submit" class="btn" value="Save" />
</form>
<body>
<html>
And I can see the dropdowns now:
Here is my base.html:
{% load static %}
{% load compress %}
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="{% static 'mism_web/css/materialize.min.css' %}" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="{% static 'mism_web/css/helper.css' %}" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<!-- TODO: Fix navbar links -->
<body>
<div>
<nav>
<div class="nav-wrapper">
MISM
<a href="#" data-activates="mobile" class="button-collapse">
<i class="material-icons">menu</i>
</a>
<ul class="right hide-on-med-and-down">
<li>
<a href="{% url 'mism_web:index' %}">
<i class="material-icons left">home</i>
Home
</a>
</li>
<li>
<a href="{% url 'mism_web:device_list' %}">
<i class="material-icons left">perm_device_information</i>
Devices
</a>
</li>
<li>
<a href="{% url 'mism_web:workflow_list' %}">
<i class="material-icons left">format_indent_increase</i>
Workflows
</a>
</li>
<li>
{% if user.is_authenticated %}
{{ user.get_username }}
{% else %}
Unknown user
{% endif %}
</li>
</ul>
<ul class="side-nav" id="mobile">
<li>
<a href="{% url 'mism_web:device_list' %}">
<i class="material-icons top">home</i>
Home
</a>
</li>
<li>
<a href="{% url 'mism_web:device_list' %}">
<i class="material-icons top">perm_device_information</i>
Devices
</a>
</li>
<li>
<a href="{% url 'mism_web:workflow_list' %}">
<i class="material-icons top">format_indent_increase</i>
Workflows
</a>
</li>
<li>
{% if user.is_authenticated %}
{{ user.get_username }}
{% else %}
Unknown user
{% endif %}
</li>
</ul>
</div>
</nav>
</div>
<div class="container">
{% block content %} {% endblock %}
</div>
<script type="text/javascript" src="{% static 'mism_web/js/jquery-3.2.1.min.js' %}"></script>
<script type="text/javascript" src="{% static 'mism_web/js/materialize.min.js' %}"></script>
<script type="text/javascript" src="{% static 'mism_web/js/helper.js' %}"></script>
</body>
</html>
So, it's the base.html interfering with the form display in some way. I have no idea how.

From what I understood. It seems you havent initialized the select in materialize.
Try adding a script tag in the html file with the form with the following contents:
$(document).ready(function() {
$('select').material_select();
});
You may need to initialize the datepicker plugin too to display the datepicker calendar.
More about this can be found here:
http://materializecss.com/forms.html

Related

Jinja - If statement not work - Please help me [duplicate]

This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 1 year ago.
I hope you can help me, because I can't find an solution on google.
Sense:
If you have logged in, you can switch to the settings otherwise not.
This is my code:
{% if settings is true %}
<a href="{{ url_for('main.settings') }}" class="navbar-item">
Settings
</a>
{% endif %}
This is the Full Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
<link rel="stylesheet" href="/css/style.css" />
</head>
<body>
<section class="hero is-fullheight" style="background-color:#C4FCEF;">
<div class="hero-head">
<nav class="navbar">
<div class="container">
<div id="navbarMenuHeroA" class="navbar-menu">
<div class="navbar-end">
<a href="{{ url_for('main.index') }}" class="navbar-item">
Home
</a>
{% if current_user.is_authenticated %}
<a href="{{ url_for('main.profile') }}" class="navbar-item">
Profile
</a>
{% endif %}
{% if settings == true %}
<a href="{{ url_for('main.settings') }}" class="navbar-item">
Settings
</a>
{% endif %}
{% if not current_user.is_authenticated %}
<a href="{{ url_for('auth.login') }}" class="navbar-item">
Login
</a>
<a href="{{ url_for('auth.signup') }}" class="navbar-item">
Sign Up
</a>
{% endif %}
{% if current_user.is_authenticated %}
<a href="{{ url_for('auth.logout') }}" class="navbar-item">
Logout
</a>
{% endif %}
</div>
</div>
</div>
</nav>
</div>
<div class="hero-body">
<div class="container has-text-centered">
{% block content %}{% endblock %}
</div>
</div>
</section>
</body>
</html>
This is the Error:
TypeError:
The view function did not return a valid response.
The function either returned None or ended without a return statement.
Try changing {% if settings is true %} to {% if settings %} or {% if settings == true %} or {% if settings is sameas true %}
Refer here

Python Django website not showing on Ubuntu like on Windows

I run into a problem without understanding where it could come from.
I have the same website in Django 3.0.8 under environment which works on one side on a PC with windows 10:
On the other side, on an old PC with Xubuntu 20.04:
Everything is the same: code, files, environments, version of python (3.8). However, the Xubuntu version doesn't perform well as you can see.
Do you have any idea what could be blocking? I have the impression that Bootstrap and FontAwesomeIcon are not working correctly.
.
EDIT : This is the code of the page.html
{% load i18n %}
{% load static %}
{% load bootstrap5 %}
{% get_current_language as language_code %}
<!DOCTYPE html>
<html lang="{{ language_code }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Une vie électrique sans barrière.">
<meta name="author" content="Pierre TREMELO">
<link rel="icon" href="{% static 'favicon.ico' %}">
<title>ElyanDeal</title>
<link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<link href="{% static 'css/styles.css' %}" rel="stylesheet">
<!-- CUSTOMING -->
<link href="{% static 'css/fontawesome.css' %}" rel="stylesheet">
</head>
<body>
<img src="{% static 'accounts/fond_accueil.jpg' %}" class="superbg" />
<nav class="navbar navbar-expand-md navbar-dark bg-dark static-top">
<a class="navbar-brand" href="/">
<i class="fas fa-home"></i> {% trans 'Home' %}
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
{% if request.user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{% trans 'Mon compte' %}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'accounts:change_profile' %}?next={{ request.path }}">
{% trans 'Profil' %}
</a>
<a class="dropdown-item" href="{% url 'accounts:change_password' %}">
{% trans 'Mot de passe' %}
</a>
<a class="dropdown-item" href="{% url 'accounts:change_email' %}">
{% trans 'Email' %}
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'accounts:log_out' %}">
{% trans 'Déconnexion' %}
</a>
</div>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'accounts:log_in' %}">{% trans 'Connexion' %}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'accounts:sign_up' %}">{% trans 'Créer un compte' %}</a>
</li>
{% endif %}
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'change_language' %}">{{ language_code|upper }}</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid mt-3">
{% bootstrap_messages %}
{% block content %}
No content.
{% endblock %}
</div>
<script src="{% static 'vendor/jquery/jquery-3.4.1.min.js' %}"></script>
<script src="{% static 'vendor/popper/popper.min.js' %}"></script>
<script src="{% static 'vendor/bootstrap/js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/ie10-viewport-bug-workaround.js' %}"></script>
</body>
</html>
And this one for the content of the {% block content %} :
{% extends 'layouts/default/page.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap5 %}
{% block content %}
<div class="center-etroit" style="background-color:transparent; max-width:500px;">
<div class="card">
<div class="card-header">
<h4>{% trans 'Connexion' %}</h4>
</div>
<div class="card-body">
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
<button class="btn btn-primary">{% trans 'Connexion' %}</button>
</form>
<hr>
<ul>
<li>
{% trans 'Mot de passe oublié ?' %}
</li>
<li>
{% trans 'Identifiant oublié ?' %}
</li>
<li>
{% trans "Renvoyer le code d'activation" %}
</li>
</ul>
</div>
</div>
</div>
{% endblock %}
I ended up finding the issu! I had installed 'django-fontawesome-5' instead of 'fontawesome-free' in my virtual environment. Now that I have reinstalled the correct package, the appearance is fixed!

Hi, I have a problem with a django app. I want do make personalized pages for users with this url: url/user/id. But something doesn't work

I'm quite new in the programming world and that's my firs real project. Usually I solve my problems just sitting and thinking till my brain burns. But this time I'm really stacked. Maybe is easy but I really didn't find solutions
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('profilo/<int:my_id>/', views.profilo, name='profilo')
]
views.py
def profilo(request, my_id):
users = User.objects.get(id=my_id)
contex = {
"user": users
}
return render(request, 'profilo/profilo.html', contex)
base.html
{% load static %}
<html>
<head>
<title>Django Boyz blog</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<div class="page-header">
{% if user.is_authenticated %}
<span class="glyphicon glyphicon-plus"></span>
<p class="top-menu">Ciao {{ user.username }} <small>(Log out)</small></p>
<span class="glyphicon glyphicon-user"></span>
{% if user.is_superuser %}
<span class="glyphicon glyphicon-inbox"></span>
{% endif %}
{% else %}
<span class="glyphicon glyphicon-lock"></span>
{% endif %}
<h1>Django Boyz Blog</h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
The error is this: NoReverseMatch at /
Reverse for 'profilo' with arguments '('',)' not found. 1 pattern(s) tried: ['profilo/(?P<my_id>[0-9]+)/$']
Thanks
In this line:
<span class="glyphicon glyphicon-user"></span>
The u should be small in User.id:
<span class="glyphicon glyphicon-user"></span>

URL to each user's profile in a list of users

I have the following code in my userprofile_list.html template:
{% extends "base.html" %}
{% block content %}
{% for users in userprofile_list %}
<a href="{% url 'users:user_profile' user.pk %}">
<div class="user-card">
<img class="profile-pic" src="{%if user.userprofile.profile_pic%}{{user.userprofile.profile_pic.url}}{%endif%}">
<p class="user-card-name">{{ users.pk }}</p>
<p class="user-card-name">{{ users.first_name }}</p>
<p class="user-card-type">{{ users.user_type }}</p>
</div>
</a>
{% endfor %}
{% endblock %}
Note the line <a href="{% url 'users:user_profile' user.pk %}">, I am using it elsewhere in the my app and when clicked it takes you to the profile of the currently logged-in user. However, I would instead like it to take you to the profile of whichever user you clicked on in the users list being created by the for loop. How do I change the url to do that?
I think what has to be done is that instead of getting the pk of the currently logged in user it has to instead get the pk of that specific user in the users list, which is then passed through to the url patterns (already working so I didn't posting it).
Note: If I'm not right with my logic on what has to happen thats fine just let me know what you need to see in order to help. Thank you.
Edit
base.html:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<title>Evverest</title>
<meta name"viewport" content="width=device-width, initial-scale=1">
<meta charset="uft-8">
<link rel="shortcut icon" href="/images/favicon.ico">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans|Mukta+Mahee|Noto+Sans" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<nav>
<div class="container">
<a class="brand" href="{% url 'index' %}">Evverest</a>
<div class="navbar">
<a class="nav-link" href="{% url 'index' %}">Home</a>
{% if user.is_authenticated %}
<a class="nav-link" href="{% url 'users:user_profile' user.id %}">
{{ user.username|capfirst }}
</a>
<a class="nav-link" href="{% url 'users:user_list' %}">All Members</a>
<a class="nav-link" href="{% url 'account_logout' %}">Logout</a>
{% else %}
<a class="nav-link" href="{% url 'account_login' %}?next=/">Login</a>
<a class="nav-link" href="{% url 'account_signup' %}?next=/">Register</a>
{% endif %}
</div>
</div>
</nav>
<div class="container">
<div class="content">
{% block content %}
{% endblock %}
</div>
</div>
</body>
</html>
I think you have a typo in that link. Try this:
<a href="{% url 'users:user_profile' users.pk %}">
You are using user.pk The loop variable is users, so you need to use users.pk, assuming that you have created a view to edit user_profile by getting pk of user_profile .
EDIT
Try this base.html:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<title>Evverest</title>
<meta name"viewport" content="width=device-width, initial-scale=1">
<meta charset="uft-8">
<link rel="shortcut icon" href="/images/favicon.ico">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans|Mukta+Mahee|Noto+Sans" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<nav>
<div class="container">
<a class="brand" href="{% url 'index' %}">Evverest</a>
<div class="navbar">
<a class="nav-link" href="{% url 'index' %}">Home</a>
{% if request.user.is_authenticated %}
<a class="nav-link" href="{% url 'users:user_profile' request.user.id %}">
{{ request.user.username|capfirst }}
</a>
<a class="nav-link" href="{% url 'users:user_list' %}">All Members</a>
<a class="nav-link" href="{% url 'account_logout' %}">Logout</a>
{% else %}
<a class="nav-link" href="{% url 'account_login' %}?next=/">Login</a>
<a class="nav-link" href="{% url 'account_signup' %}?next=/">Register</a>
{% endif %}
</div>
</div>
</nav>
<div class="container">
<div class="content">
{% block content %}
{% endblock %}
</div>
</div>

Django not displaying block content

I'm fairly new to Django and I'm trying to set up my project so that there's a homepage and then off of the homepage a bunch of different links. The problem is whenever I click one of my other tabs (products, login, contact) etc. it just reloads the homepages block content despite the fact that I told it to load something else. so when I click the links it does show that I'm on a new webpage (ill see whatever my BASE_DIR is with '/courses/products/' for example in the url bar but its always the same website which is displaying views.homepage.
Here's my courses/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'$', views.homepage, name='homepage'),
url(r'contact/$', views.contact, name='contact'),
url(r'login/$', views.login, name='login'),
url(r'products/$', views.products, name='products'),
url(r'register/$', views.register, name='register'),
]
here's my layout.html which is in the template folder:
{% load static from staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>HealthSmart</title>
<link href="{% static 'images/photo.jpg' %}" rel='icon' type='image/x-icon' />
<link rel="stylesheet" href="{% static 'css/normalize.css' %}" />
<link href="http://fonts.googleapis.com/css?family=Lora|Open+Sans:400italic,700italic,400,700,300,800" rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Varela+Round|' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="{% static 'css/pavle.css' %}" />
<link rel="stylesheet" href="{% static 'css/responsive.css' %}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<a href="{% url 'courses:homepage' %}" id="logo">
<h1>{% block title %} {% endblock %}</h1>
</a>
<nav>
<ul>
<li>Home</li>
<li>Products</li>
<li>Contact</li>
<li>Login</li>
</ul>
</nav>
</header>
{% block content %} {% endblock %}
<div id="footer">
<footer>
<img src="{% static 'images/briggs.png' %}" alt="BriggsLogo" class="social-icon">
<p>©2015 HealthSmart Prototypes.</p>
</footer>
</div>
</body>
</html>
and finally, here's my views.py:
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from .models import Owner, Device, Page
# Create your views here.
def homepage(request):
home = Page.objects.all()
return render(request, 'courses/homepage.html', {'homepage': homepage})
def contact(request, contact_pk):
contact = get_object_or_404(Page, pk=contact_pk)
return render(request, 'courses/contact.html', {'contact': contact})
def login(request, login_pk):
login = get_object_or_404(Page, pk=login_pk)
return render(request, 'courses/login.html', {'login': login})
def products(request, products_pk):
product = get_object_or_404(Page, pk=products_pk)
return render(request, 'courses/products.html', {'product': product})
def register(request, register_pk):
register = get_object_or_404(Page, pk=register_pk)
return render(request, 'courses/register.html', {'register': register})
Also I'll include the homepage.html and products.html just in case:
homepage.html:
{% extends "layout.html" %}
{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
<div class="main_body">
<div class="hep">
<section class="product">
<h3 id="purify">Purifying your life</h3>
<p id="smartprod">Meet our smart products today.</p>
</section>
</div>
</div>
<div id="information">
<h3>Change your Home Environment with the new Briggs HealthSmart Air Purifier and Humidifier</h3>
<p>Run multiple devices at the same time all on your smartphone!</p>
</div>
<div id="getStarted">
<p>If you have an account Login, or you can get started today and Register!</p>
</div>
</article>
{% endblock %}
products.html:
{% extends "layout.html" %}
{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
<div id="wrapper">
<section>
<ul id="gallery">
<li>
<a href="{% static 'images/air purifier 03272015.png' %}">
<img src="{% static 'images/air purifier 03272015.png' %}" alt="" />
<p>The Briggs Air Purifier.</p>
</a>
</li>
<li>
<a href="{% static 'images/base 03272015.png' %}">
<img src="{% static 'images/base 03272015.png' %}" alt="" />
<p>The Briggs Base.</p>
</a>
</li>
<li>
<a href="{% static 'images/humidifier 03272015.png' %}">
<img src="{% static 'images/humidifier 03272015.png' %}" alt="" />
<p>The Briggs Air Humidifier.</p>
</a>
</li>
<li>
<a href="{% static 'images/water tank 03272015.png' %}">
<img src="{% static 'images/water tank 03272015.png' %}" alt="" />
<p>The Briggs Water Tank.</p>
</a>
</li>
</ul>
</section>
</div>
</article>
{% endblock %}
use
url(r'^$', views.homepage, name='homepage'),
for your homepage, can you also show us how you extends your "layout.html" ?
For your contact problem you can add the pk in your url pattern
url(r'contact/(?P<contact_pk>\d+)/$', views.contact, name='contact'),

Categories