How to declare and use global variables in Django (with Eclipse) - django

I'm developing an application that needs a global variable. Fortunately, it is working despite Eclipse complaining about the way I used the global variable. To use as an example in this question, I created an application (which works!) that uses a global variable as a page counter. Here is the code:
My __init__.py file:
counter = 0
My views.py
from AccessCounter import counter
from django.shortcuts import render
def conterf(request):
global counter
counter +=1
context = {
'counter' : counter,
}
return render(request, 'AccessCounter/index.html', context)
And Eclipse is complaining that I have a "Unsed import: counter" at line "from AccessCounter import counter", but if I remove this line, the counter does not work with this error:
name 'counter' is not defined
I don't think that the following information is relevant, but here they are...
My index.html file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Access counter: {{ counter }}
</body>
</html>
and my url.py file:
from django.contrib import admin
from django.urls import path
from AccessCounter import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.conterf, name='counter'),
]

Related

Django forms are not being processed with POST request

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!

Unable to get include js file

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

Django Linking a html page to a view

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' %}'

Django url configuration error

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

Using variable from settings.py in layouts

I am from PHP,Zend world and new to django python. It might be a dump question, but I tried several ways, it didn't work, please advice me .
I am using jinja2 with Django 1.7. I am trying to put the 'APPLICATION_TITLE' in settings.py
APPLICATION_TITLE = "My Application"
I want this value to be used in layout.jinja.html as the title
layout.jinja.html
<head>
<!-- APPLICATION_TITLE should go below -->
<title>{{ ???????????? }}</title>
</head>
Please guide me.
You can create a context processor:
your_app/context_processors.py
from django.conf import settings
def application_title(request):
return {'APPLICATION_TITLE': settings.APPLICATION_TITLE}
settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
...
"your_app.context_processors.application_title",
)
layout.jinja.html
<head>
<title>{{ APPLICATION_TITLE }}</title>
</head>