I have a html that contains some forms and buttons. One button corresponds to a file that I have uploaded. I want to achieve when I click on a button, my views will act on the corresponding file, and shows the results in another html like picture below.
How can I give each button specific parameters from my models? and how to get the parameter in my views.
I tried this code, but failed to get it. is this right?
<button type="submit" class="btn btn-info btn-xs" name={{item.description}}></button>
You must do something like that:
You should add new route for your new page to routes.py:
from . import views
urlpatterns = [
...
url(r'^new-page/$', views.new_page_view, 'new_page'),
]
after that your views.py must have your view function, that will execute when you go to url /new-page/
views.py:
from django.shortcuts import render
def new_page_view(request, question_id):
return render(request, 'path/to/your/template')
You can read more about render() there
In your template (When you want to add your link button) you must add following string:
Link to new Page
Something loke that you must do with your project.
More about this you can read there
I hope this answer will help you
Related
I have developed a funcktion that deletes several entries in my django project. They are bases on marked checkbox values. When all done, I want to refresh my template page so that all premarked checkboxes diseapear. But when trying redirect and render, nothing really happens. I want to refresh the page as if I clicked refresh on the page. How do I do that?
views.py
def deleteMulti(request):
g=request.GET
checked=g.getlist('marked[]')
dbase=g.get('dbase')
print('delete: ',checked)
print('dbase: ',dbase)
#res=[Concert.objects.get(pk=l) for l in checked]
if dbase=='concert':
deleted=[Concert.objects.filter(id=l).delete() for l in checked]
res=Concert.objects.all()
print('delete')
response=redirect('events')
return response
urls.py:
path('events',Events.as_view(),name='events'),
I want to refresh the page as if I clicked refresh on the page.
it seems you have a button in your page that you want to perform refresh page action on it. actually it done by JS easily as
<button onclick="location = self.location.href;"> Refresh </button>
but if you want to do it by django you can do something like this
from django.http import HttpResponseRedirect # import
from django.urls import reverse # import
# other codes...
if dbase=='concert':
deleted=[Concert.objects.filter(id=l).delete() for l in checked]
res=Concert.objects.all()
print('delete')
return HttpResponseRedirect(reverse('event')) # redirect
My intention is to change from interface view -> switch view to process some data and send those data and change to -> test view to display the result. However, nothing in switch view seems to be processed and switch view doesn't change to test view after I hit 'submit' on userInterface.html. My guess is that the problem lies on the HttpResponseRedirect() function or anything related to url paths. Everything worked find with my other project that I worked on my computer. I'm not sure what I need to change to use Django on RaspberryPi.
At first, I found out I didn't import libraries needed for those function. After I imported them, the code was still not working.
I commented out other codes in switch view that do nothing with changing views and just focus on changing view in my switch view.
view.py
def user_interface(request):
return render(request,'zuumcoin/userInterface.html',
{})
def switch(request):
return HttpResponseRedirect(reverse('zuumcoin:test'))
def test(request):
return render(request,'zuumcoin/test.html',{})
userInterface.html
....
<form action="{% url 'zuumcoin:swicht' %} method = "POST">
{% csrf_token %}
...
...
</form>
urls.py
app_name='zuumcoin'
urlpatterns = [
url(r'', views.user_interface, name='interface'),
url(r'switch/', views.switch, name='switch'),
url(r'test/', views.test, name='test')
]
I expect HttpResponseRedirect to direct me to test view instead of being stuck in switch view. If it can do that, I think I can find a way for other part of my code in my switch view to run.
You didn't terminate your regexes. So the first pattern matches every path.
You should do:
url(r'^$', views.user_interface...)
It seems you have typo in your userInterface.html template. change this:
{% url 'zuumcoin:swicht' %}
to this one:
{% url 'zuumcoin:switch' %}
I have a index.html page with a list of "cards", where each card have a "Click to Select" link.
When user click in this link i'd like to call a function in python to select this item, see:
def selectItem(request, item):
#so something with this item
so, in my html pagE:
<div class="card-action">
Selecionar
</div>
This don't work. What is the right way to do it ?
You can not call a function like that. A browser requests data with an HTTP request, and the server answers with an (HTTP) response. Such requests have a URL, and Django can route the request - with the URL - to the right view that will calculate a response.
We thus need to construct a view that can then be called. Your call is already quite close:
# app/views.py
from django.http import HttpResponse
def select_item(request, item_id):
# so something with this item_id
# ...
return HttpResponse()
Since most objects are not serializable (and usually you do not want that anyway, since it would expose a lot of (potentially sensitive) data to the user, we thus need to work with an id (an identifier that is for example stored in the database that corresponds to an object).
The response contains the data in the response. Frequently that is HTML code that is then rendered by the browser.
Now in urls.py, we can specify how the url looks like, for example:
# app/urls.py
from django.urls import path
from app.views import select_item
urlpatterns = [
path('select_item/<int:item_id>/', select_item, name='select_item_view'),
# ...
]
The urlpatterns need to be included in the root urlpatterns (in the project root).
Now in the HTML template, we can generate the URL that matches with this view, something similar to:
<div class="card-action">
Selecionar
</div>
Django will then make sure that the href points to an URL that refers to the select_item view with the correct parameter.
hello how can i run script that i build on python when i click button on form html
i saw examples like this but i dont get it:
Html code: index.html
<form action="{% url my_view %}" method="post">
<input type="submit" value="Ok">
</form>
views.py
import django.shortcuts import render
def my_view(request):
if request.method == 'POST':
import set_gpio
#my url is "templates/load.py"
return #How can i return?
You need to return an HttpResponse object from your view. You can return one response inside your if statement, and another outside of it (so anything but a POST). Usually the render shortcut is used to render a response from a template. So your view would be something like:
import django.shortcuts import render
def my_view(request):
if request.method == 'POST':
# The user clicked the button (POST)
return render(request, "load.html")
# The user is just loading the view for the first time (GET)
return render(request, "index.html")
I highly suggest going through the django tutorial.
You have to realise that Django is a HTTP based framework meaning that when an HTTP request is sent to a url, Django will be allowed to perform an action. So, every function is actually a reaction to the HTTP requests that the user sends to your urls.
Also, if you are using Django template system, the html file should be rendered by Django in the first place. So, you cannot have a standalone html file with {% url my_view %} in it.
First, you have to configure the main urls.py which is in a folder with the same name as your project.
Second, in your app, create an urls.py file and configure it.
At the end, you connect your my_view to a specific or set of urls and will be triggered when a request is sent to that url, whether GET or POST.
These are somewhat large (and easy) topics for which you have to watch a tutorial or read the Django documentations.
I tried to make a multi-upload system. I have a functionnal page music.html, which list all musics and have some buttons to access to another page to add music.
'Add Music' should be the button's name instead of 'Music'.
music.html :
<legend class="scheduler-border"><span class="glyphicon glyphicon-plus"></span> Add Music </legend>
However, I don't need to execute any functions from music.html to addmusic.html...
My urls.py :
url(r'^music/$','webgui.views.music'),
url(r'^addmusic/$','webgui.views.addmusic'),
url(r'^addmusic/add/$', 'webgui.views.multiple_uploader'),
Is there a way to redirect music.html to addmusic.html without executing 'webgui.views.addmusic' ??
Really, I just need to display the form.
EDIT :
Yes, finally it works with my addmusic function in views.py :
def addmusic(request):
return render(request, 'addmusic.html')
So it means an URL necessarily have a corresponding function ?? I can't pass "href=addmusic.html" in my template ? Just to know ...
You can use TemplateView.
from django.views.generic import TemplateView
url(r'^addmusic/$', TemplateView.as_view(template_name="addmusic.html"), name='add-muusic'),