trying to refresh page in django from view not working - python

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

Related

How can I make Django user logout without a new url page?

For logout I am using ready django.contrib.auth.LogoutView and everyone in their tutorials are creating a new url for example '/logout' and my question is how can I use that view without a new url page and html template? Plese use some code for views.py in your answers ;)
If you don't want any redirects at all and want to stay on the same page, like when you send an AJAX request then you can write your own view like this:
from django.contrib.auth import logout
from django.http import HttpResponse
def logout_view(request):
logout(request)
return HttpResponse('OK')
You don't have to route the /logout url to a template. You can redirect it to whatever page you want by using the next_page attribute (docs).
You can also look into setting a LOGOUT_REDIRECT_URL: https://docs.djangoproject.com/en/3.1/ref/settings/#logout-redirect-url.

How to execute script python when i click button in form html?

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.

Django html interacts with views

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

reverse function does not change the url to request a different view, django

I have a form that a user fills and then clicks a button to submit, then it calls a transition page, to inform the user that the form was completed. The user can click a button to go back to the home page from the transition page.
I want to get rid of the transition page and go to the home page directly. The reverse function does not change the URL but renders the correct homepage template. However, the context data in the template does not get populated and I am assuming that the URL not changing causes that.
The homepage urls.py I include:
url(r'^(?P<user_id>\d+)/$', views.UserHomePageView.as_view() ,
name='user-homepage'),
Example: the form URL is
localhost:8000/form/15/fill
After the form is submitted, I want it to redirect to
localhost:8000/home/3
from the view after form submission, I call
return HttpResponseRedirect(reverse('homepage:user-homepage', args=[userid]))
try this:
return redirect('home')

clicking a link cause running a view

I have a view for logout,now I wanna when user click on an anchor link the logout view run,how can I do this?
in views.py:
def logout(request):
auth.logout(request)
return render_to_response("airAgency/index.html")
I'm new in django,I can submit a form and run a view for that, but I
don't now what should I do to run s.th like logout view when clicking
an anchor link.
Logging out via a link instead of a form is easy.
https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.logout
from django.contrib.auth import logout
def logout_view(request):
logout(request)
return http.HttpResponse("You've been logged out")
Just point a url to this view like you did with your form and go to that URL.

Categories