I want to include below js file to my template but unable to do so. It includes rest of links which are not passing values but this is not including because it passing value v=1.4.0.
I am new to django please help me out
js link
<link href="{% static 'djangoapp/light_css_js/light_css/light-bootstrap-dashboard.css?v=1.4.0' %}" rel="stylesheet"/>
url.py:
from django.urls import path
from . import views
urlpatterns = [
path('test1', views.test1, name='profile1'),
]
views.py
def test1(request):
return render(request, 'user.html')
Error
"GET /static/djangoapp/light_css_js/light_js/light-bootstrap-dashboard.js%3Fv%3D1.4.0 HTTP/1.1" 404 1832
Related
I just simply want to show my static image in template.
I have looked every possible ways to solve this issue and try various approaches but could not get my static images in template. The alternative text of the respective image is shown but image itself not loaded and get this in terminal Window
"GET /static/image/down.jpg HTTP/1.1" 404 1771
here how my Django project is laid out is given below
project files hierarchy
in urls.py
from django.conf.urls import include,url
from django.contrib import admin
from django.urls import path
from myapp import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
.......]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL= "/media/"
in template question.html
i have tried different approaches to access the image[with {% load static %} tag on the top of html page]
like
<img src="{% static 'image/down.jpg' %}">
<img style="width: 245px; height: 247px;"
alt="Image description"
src="{% static 'image/down.jpg' %}"/>
<img src="/static/image/up.jpg" alt="Second-image" />
but could not get the problem solved
Make sure you have pillow installed correctly and if it still wont work, try reinstalling pillow.
Did you run: python manage.py collectstatic?
If not work, try:
{% load staticfiles i18n %}
In addition check django.contrib.staticfiles is included in your INSTALLED_APPS
I have written the most basic Django application to understand forms as below. When I enter the required data into the fields and press Submit, the code after "if request.method == 'POST'" is ignored. I am redirected to the appropriate page and an entry with the first name "John" and last name "Smith" is created in my database. As you can see in the code below, this object should only be created if the request method is not POST. I know that I have set the request method to POST because that is what is shown on my CMD so what is happening??
Here is my template 'index.html':
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the site</title>
</head>
<body>
<form action="thanks/" method='POST'>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" vlaue="Submit">
</form>
</body>
</html>
Here is my views.py file:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import NewObjectForm
from .models import Object
# Create your views here.
def index(request):
if request.method == 'POST':
form=NewObjectForm(request.POST)
if form.is_valid():
first_name=form.cleaned_data['first_name']
last_name=form.cleaned_data['last_name']
a=Object.create(first_name=first_name,last_name=last_name)
a.save()
return HttpResponseRedirect('/thanks/')
else:
new=Object.create(first_name="Not",last_name="Valid")
new.save()
else:
#Code which is run if the request.method is not equal to 'POST'
form=NewObjectForm()
newer=Object.objects.create(first_name="John",last_name="Smith")
newer.save()
return render(request,'formapp/index.html',{'form':form})
def end(request):
return render(request,'formapp/thanks.html')
Here is the urls.py file from the main project:
"""experimentalForms URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('',include('formapp.urls')),
path('admin/', admin.site.urls),
]
And here is the urls.py file from my application:
'''
URLs for formapp
'''
from django.urls import path
from . import views
app_name="formapp"
urlpatterns=[
path('',views.index,name='index'),
path('thanks/',views.end),
]
Thanks #mbieren! I have changed my the action attribute of my form and it is working perfectly!
I am new to Django and Vue.js, and trying to combine those tow frameworks, So I followed a video tutoriel and copy every thing he does, except for the names of project, app, and variables, and had to copy only the webpack.config.js. So in the video, he made homepage/games as his main page, but I choosed to make webapp/game as my main page.
and when ever I tipe http://127.0.0.1:8000/ in the browser, the data I extracted from my database are not showen and the PowerShell shows this error
[06/Apr/2018 15:25:26] "GET / HTTP/1.1" 200 220
Not Found: /homepage/games
I don't rmember copying or writing homepage in any of my project files.
and I really don't know whitch code I should post in here so you could answer me.
So please if anyone have any idea or wants me to post any code,please let me know in a comment.
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index , name="index"),
url(r'^webapp/game$', views.game , name="game"),
]
views.py
from django.shortcuts import render
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import games
def index(request):
game= games.objects.all()
return render(request, "webapp/index.html", {"game": game})
def game(request):
game= serialize("json", games.objects.all())
return HttpResponse(game, content_type= "application/json")
index.html
{% extends "layout.html" %}
{% block content %}
<h1> hello </h1>
<div class="exemple">
<exemple></exemple>
</div>
{% endblock %}
PS: here is the link of the video I followed
https://www.youtube.com/watch?v=rxLZg4PqC8M
So I know that there are other people who have asked the same question, and I have read through them. However, the solutions provided there are giving me a strange error, and I would appreciate any help in understanding it.
So here's my home.html file:
<head>
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<!-- Sign Up -->
Sign Up
</body>
And here's my views.py:
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, "home.html")
def signup(request):
return render(request, "signup.html")
And here's my urls.py:
from django.conf.urls import url
from .views import home, signup
urlpatterns = [
url(r'^signup/', signup, name="signup"),
url(r'^', home, name="home"),
]
Thank you for all the help :)
Edit:
The error message is
Reverse for 'signup' not found. 'signup' is not a valid view function or pattern name.
Also I actually changed the way I did urls.py. Now, I only have one urls.py in my main mysite folder:
from django.conf.urls import url, include
from django.contrib import admin
from home import views
from accounts import views as accountsViews
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home),
url(r'^signup', accountsViews.signup),
]
Your second url in 'urls.py' does not have a name.
The url tags would not be able to find them by name -- '{% url 'signup' %}'
so I have created a Django application with three html pages. I want from a page which is not the homepage to another. However, the there seems to be some mix up in the url when the server tries to access the latter page.
Here's my application urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow.html/$',views.findnow,name='findnow'),
url(r'^more.html/$',views.more,name='more')
]
I wish to go from "findnow.html" to "more.html". I want the url to be "localhost:port/more" but instead the server goes to "localhost:port/findnow.html/more.html".
Here's my html code snippet for findnow:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
Here's my views.py:
def more(request):
return render(request,'myapp/more.html')
So adjust it to be:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow/$',views.findnow,name='findnow'),
url(r'^more/$',views.more,name='more')
]
And your template:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
This is happening because you are linking more.html as a relative page.
You should either prepend a / to it:
MORE
Or reverse it using the url tag instead:
MORE