Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
"""
this is my view.py file in which i created in order to make view on my html document
from urllib import response
from django.shortcuts import render
from django.http import HttpRequest
def index(request):
return response(request,'good/try.html')
and also i create the path in urls.py file
from django.urls import path
from. import views
urlpatterns = [
path('',views.index)
]
My simple html document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>
many web sites still in their infancy. Various versions
have evolved over the years, sometimes by accident, sometimes on purpose
(injected humour and the like).
</p>
</body>
</html>
but when I try to render my html page it raise an error which was:
TypeError at /
'module' object is not callable in the browser
You're mistakenly importing the urllib.response module and trying to call it.
return response(request, 'good/try.html')
should be
return render(request, 'good/try.html')
Related
I'm pretty sure that could be possible by customising the django admin site for that specific feature.
I'm trying to add this functionality using admin.py but no better luck since a week.
In the picture you can see I put a circle where I will like to add and icon that will show a tooltip saying information about that specific field or column.
List View Image
So is there any way to do it easily without customising it from templates. Because the List view is so much complex and we do not want to complicate the things doing it hard way.
I tried to find it online and in the django official docs but every time its about customising it from templates, also I can html from admin.py but it doesn't invokes the tooltip as I wanted.
The short answer is: No, django can not give you a popover inside of your ListView. Django takes care about the backend. Its purpose is to serve the content of the tooltip from database to your html template. The displaying part is the job of the frontend. Therefore you have to design your tooltip yourself using html, css and javascript.
A useful framework is bootstrap as it takes care about the javascript and css. I will give you an example.
models.py
class MyModel(models.Model):
info_field = models.CharField(max_length=50)
views.py
def my_list_view(request):
my_objects = MyModel.objects.all()
return render(request, 'my_app/list-view.html', context={'my_objects': my_objects})
list-view.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
{% for object in my_objects %}
<a tabindex="0" class="btn" role="button" data-bs-toggle="popover"
data-bs-trigger="focus" data-bs-content="{{ object.info_field }}" data-bs-html="true">
</a>
{% endfor %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"></script>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]');
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));
</script>
</body>
I have this piece of code that is working fine on my PC environment (virtual environment) but causing problems in a ubuntu, Nginx server. It is a digital ocean droplet actually. I am not very much familiar with ubuntu that's why I am unable to understand the cause of the problem.
Now the problem is, that the 'checkout.html' template doesn't render the value of 'fire'. I have tried to troubleshoot manually and in the 'inspect' window I only see an empty label.
Why is this happening, is it because of the django=2.2.3 version or ubuntu or Nginx or am I missing anything here?
Note: I am updating the files through Filezilla.
Kindly help, please.
Views.py
def checkout(request):
return render(request, 'checkout.html', {'fire':"fire"})
checkout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<label>{{fire}}</label>
</body>
</html>
urls.py
from .views import *
path('checkout-page', checkout, name="checkout"),
I am beginner on django and trying to create a profile model that interrelate "User" built-in model via One-To-One relation field, it results impossible to span the user model properties by profile model instance and vise versa.
Can someone help me.
models.py
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
users=models.OneToOneField(User, on_delete=models.CASCADE)
image=models.ImageField(default='profilepic.jpg',upload_to='profile_pictures')
location=models.CharField(max_length=100)
def __str__(self):
return self.users.username
profileTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
</head>
<body>
<h2>{{user.username}}</h2>
<img src='{{user.Profile.image}}'>
</body>
</html>
use a lowercase profile as per the docs
<img src='{{user.profile.image}}'>
I am using Django 3.0.8 My view function is as follows:
from django.shortcuts import render import datetime
# Create your views here.
def date_time_view(request):
date=datetime.datetime.now()
h=int(date.strftime('%H'))
if h<12:
msg='Hello Guest!!! Very good Morning!!!'
elif h<16:
msg='Hello Guest!!! Very good Afternoon !!!'
elif h<21:
msg='Hello Guest!!! Very good Evening!!!'
else:
msg='HELLO GUEST!! very good Night!!!'
my_dict = {'date':date,'msg':msg}
return render(request, 'testapp/results.html', my_dict)
and my template is as follows:
<!DOCTYPE html> {%load staticfiles%}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>hello</title>
</head>
<body>
<h1>{{msg}}</h1>
<h2>{{date}}</h2>
<img src="{%static " images/images.jpg" %}">
</body>
</html>
Check your configuration in your project settings file.
Templates can reside in each app separately, but also in a templates folder in APPS_DIR (folder with all Django apps).
Here is official docs for Django 3 templates:
https://docs.djangoproject.com/en/3.0/topics/templates/
Check your template's location. It should be yourproject/yourapp/templates/testapp.
I've fired up a new Django 1.8 project and am hitting the wall of trying to serve static files in the development environment. I'm pretty sure I've followed the docs explicitly, but it's just not working.
Settings.py
STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static/'),
BASE_DIR
)
STATIC_URL = '/static/'
STATIC_ROOT = '/srv/www/cpm2/static/'
urls.py:
from django.conf.urls import include, patterns, url
from django.conf import settings
from django.conf.urls.static import staticĀ·
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^test/', TemplateView.as_view(template_name="basics/base.html")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
templates/basics/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% load staticfiles %}
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
When I go to http://my_server_ip:8000/test/ none of the CSS is loaded. Viewing the page source, I see this where that CSS link is <link href="/static/css/bootstrap.css" rel="stylesheet"> which is good, but when I try to follow that link directly, it fails giving me a 404 error.
Now, I've set up my Apache server to serve the files directly, they work. So, going to http://my_server_ip/static/css/bootstrap.css works - so I know the files are readable. It's just they don't work in development.
What am I doing wrong? As so many others before me, I'm pulling my hair out!
EDIT Trying to access the file directly gives me this:
Page not found (404)
Request Method: GET
Request URL: http://my_server_ip:8000/static/css/bootstrap.css
Raised by: django.views.static.serve
'css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
It doesn't even show a list of places its tried, just that the file isn't found.
You are missing an 's' from STATICFILES_DIRS.
Also, you shouldn't be including your BASE_DIR in that setting - that could end up serving all your python code, which would be a bad idea.