The goal here is to add a delete button beneath every post, and once it is clicked, the corresponding row will be deleted from the table in the database.
views.py
from django.shortcuts import render, redirect
from .forms import CreatePostForm
from django.contrib import messages
from .models import CreatePost
def create_post(request):
if request.method == 'POST':
form = CreatePostForm(request.POST)
if form.is_valid():
post = form.save(commit = False)
post.save()
messages.success(request, f'The post has been created.')
return redirect('home_page')
else:
form = CreatePostForm()
return render(request, 'post/create_post.html', {'form': form})
def view_post(request):
context = CreatePost.objects.order_by('-dateCreated')
return render(request, 'post/view_post.html', {'context': context})
So far, there is one page, create_post.html, that allows users to create a post. There is a second page, view_post.html, that allows users to see all posts with the most recently added first. I want to put a delete button beneath every post in view_post.html.
view_post.html
{% extends "home_page/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Volunteer Opportunities</h1>
{% for x in context %}
<h4>
{{ x.task }}
<small class="text-muted">Date Required: {{ x.dateRequired }}</small>
</h4>
<blockquote>
<p>{{ x.description }}</p>
<p>Date Posted: {{ x.dateCreated }}</p>
</blockquote>
<hr>
{% endfor %}
{% endblock content %}
I'm not sure if this is necessary, but below are models.py and the project urls.py file.
models.py
from django.db import models
class CreatePost(models.Model):
dateCreated = models.DateTimeField(auto_now_add = True)
task = models.CharField(max_length = 1000)
description = models.TextField()
dateRequired = models.DateField(null = True)
urls.py
from django.contrib import admin
from django.urls import path, include
from home_page.views import home
from post.views import create_post, view_post
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name = 'home_page'),
path('create_post/', create_post, name = "create_post"),
path('view_post/', view_post, name = "view_post"),
]
Try this.
In your url
path('delete/<int:id>', view_post.destroy),
In Your html inside for
Delete
views.py
def destroy(request, id):
post = CreatePost.objects.get(id=id)
post.delete()
return redirect("/yourhtml")
Related
Good afternoon all,
One of my form does not seem to save when submitted. I cannot see why, in particular as I have a similar form working just fine using the same code.
For some reason it work just fine using the admin panel.
My assumption is that I am missing something that tells the form it needs to be saved. But cannot find what.
Any ideas?
Models
RATING=(
(1,'1'),
(2,'2'),
(3,'3'),
(4,'4'),
(5,'5'),
)
class ProductReview(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
product=models.ForeignKey(Product,related_name="comments", on_delete=models.CASCADE)
review_text=models.TextField(max_length=250)
review_rating=models.IntegerField(choices=RATING,max_length=150, default=0)
date_added = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
Views
def add_review(request, product_id):
product = Product.objects.get(pk=product_id)
form = ReviewAdd(request.POST or None, instance=product) #instance=product (populate field with existing information)
if form.is_valid():
form.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
URL
from django.urls import path
from . import views
urlpatterns = [
...
path('product/add_review/<product_id>', views.add_review,name="add_review"),
]
Forms
class ReviewAdd(forms.ModelForm):
class Meta:
model = ProductReview
fields = ('review_text', 'review_rating')
labels ={
'review_text': '',
'review_rating': '',
}
widgets = {
'review_text': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Review'}),
}
Admin
from django.contrib import admin
from .models import Venue, User, Product, ProductReview
from django.urls import path
admin.site.register(User)
admin.site.register(ProductReview)
class ProductReview(admin.ModelAdmin):
list_display=['user','product','review_text','get_review_rating']
HTML Page
{% extends 'main/base.html' %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock %}
{% block content %}
<center>
<h1>Add ReviewTo Database</h1>
<br/><br/>
{% if submitted %}
Success!
{% else %}
<form action="" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="Submit" value="Submit" class="btn btn-secondary">
</form>
{% endif %}
</center>
{% endblock %}
I detect 2 fixes
On your url, the parameter product_id might need the type of data it will going to receive
path('product/add_review/<int:product_id>', views.add_review,name="add_review"),
And in your view, you are sending an instance, and no data for a new rating.
In your view:
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
#login_required
def add_review(request, product_id):
product = get_object_or_404(Product, pk=product_id)
form = ReviewAdd(request.POST or None)
if form.is_valid():
new_rating = form.save(commit=False)
new_rating.product = product
new_rating.user = request.user
new_rating.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
I am new to Django and I am having trouble implementing the edit template to my project. I am encountering the following error:
Reverse for 'all_clients' with keyword arguments '{'client_id': 3}' not found. 1 pattern(s) tried: ['clients/all_clients/$']
I have looked on the site for similar occurrences such as Reverse for 'plan_edit' with keyword arguments
but I haven't been able to pin point the issue. I believe the issue arises when I add a hyperlink to my all_clients.html template. Also, the template pages for /clients/edit_client/?/ will load, however after submission using the save changes button the NoReserse Match error resurfaces as it attempts to load the clients/all_clients page.
See code below:
models.py
from django.db import models
# Create your models here.
class Client(models.Model):
#A client is composed of the company general info
text = models.CharField('Company Name',default = 'Company Name', max_length = 200)
phone_num = models.CharField('Phone Number', default = '000-000-000', max_length = 12)
ceo_name = models.CharField ('CEO', max_length = 50)
num_employees = models.IntegerField('Number of Employees', default = 0)
maintenance_schedule = models.CharField('maintenance schedule', max_length = 100)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
return self.text
urls.py
"""Defines URL patterns for clients."""
from django.urls import path from django.conf.urls import url
from .import views
app_name = 'clients' urlpatterns = [
#Company Page
path('index/', views.index, name = 'index'),
#Page for listing all clients
path('all_clients/', views.all_clients, name = 'all_clients'),
#Page for adding a new client
path('all_clients/<int:client_id>/', views.add_client, name = 'add_client'),
#Page for adding a new client office using a form
path('new_office/', views.new_office, name = 'new_office'),
#Page for a company to edit their entry.
path('edit_clients/<int:client_id>/', views.edit_client, name = 'edit_client'),
]
view.py
from django.shortcuts import render, redirect
from .models import Client, Location, Lease, Soft_Service, Hard_Service, Safety_Service
from .forms import ClientForm
# Create your views here.
def add_client(request, client_id):
"""Comapany page for updating facilities info"""
client = Client.objects.get(id = client_id)
context = {'client':client}
return render(request, 'clients/add_client.html', context)
def all_clients(request):
'''Shows list of all clients'''
all_clients = Client.objects.order_by ('date_added')
context = {'all_clients':all_clients}
return render(request, 'clients/all_clients.html', context)
def index(request):
"""Test Page"""
return render(request, 'clients/index.html')
def edit_client(request, client_id):
"""Edit an existing Entry."""
client = Client.objects.get(id=client_id)
if request.method != 'POST':
#Inital request; pre-fill form with the current company info.
form = ClientForm(instance=client)
else:
# Post data submitted; process data.
form = ClientForm(instance=client, data=request.POST)
if form.is_valid():
form.save()
return redirect('clients:all_clients' , client_id=client.id)
context = {'form': form, 'client': client}
return render(request, 'clients/edit_client.html', context)
edit_client.html
{% extends "app/layout.html" %}
{% block content %} {% load staticfiles %} <p>Company: {{ client }}</p>
<h4>See Our Clients</h4>
<<form action="{% url 'clients:edit_client<client_id>' client.id %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save changes</button> </form>
{% endblock %}
all_clients.html
{% extends "app/layout.html" %}
{% block content %}
{% load staticfiles %}
<div class="d-flex" style="height:75px"></div>
<div class="btn bg-white text-lg-left" style="width:425px">
<h4>See Our Clients</h4>
<ul>
{% for add_client in all_clients %}
<li>
{{ add_client }}
</li>
{%empty %}
<li> No clients have been added yet. </li>
{% endfor %}
</ul>
<a class="btn btn-secondary" href=" {% url 'clients:new_office' %}">Add a new location</a>
<a class="btn btn-secondary" href=" {% url 'clients:edit_client' client.id %}">Add a new location</a>
</div>
{% endblock content %}
First thing i think you should try is modifying the URL to the add_clients page, aside from the id you are passing is identical to all_clients, and "django may get confused":
#Page for listing all clients
path('all_clients/', views.all_clients, name = 'all_clients'),
#Page for adding a new client
path('add_clients/<int:client_id>/', views.add_client, name = 'add_client'),
instead of:
#Page for listing all clients
path('all_clients/', views.all_clients, name = 'all_clients'),
#Page for adding a new client
path('all_clients/<int:client_id>/', views.add_client, name = 'add_client'),
This is the error I get when clicking on Edit post under any one of the posts. Would appreciate any help as all this django stuff is confusing me but trying my best to learn. My new post function works and clicking blog/posts to go to the overview page for the blog or to look at all the posts works as well.
NoReverseMatch at /edit_post/1/
Reverse for 'posts' with arguments '(1,)' not found. 1 pattern(s) tried: ['posts/$']
Error during template rendering
In template C:\Users\seng\Desktop\Python projects\c19\nineteen_one\blogs\templates\blogs\base.html, error at line 0
urls.py
"""Defines url paterns for blogs"""
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns =[
#Home page
path('', views.index, name='index'),
# Page that shows all posts/
path('posts/', views.posts, name='posts'),
#Page for adding a new blogpost
path('new_post/', views.new_post, name='new_post'),
#Page for editing a post
#maybe remove the id?
path('edit_post/<int:post_id>/', views.edit_post, name='edit_post'),
]
views.py
from django.shortcuts import render, redirect
from .models import BlogPost
from .forms import BlogPostForm
# Create your views here.
def index(request):
"""The home page for blogs"""
return render(request, 'blogs/index.html')
def posts(request):
"""Show all blogposts"""
posts = BlogPost.objects.order_by('date_added')
context = {'posts': posts}
return render(request, 'blogs/posts.html', context)
def new_post(request):
"""Add a new blogpost"""
if request.method != 'POST':
#No data submitted; create a blank form.
form = BlogPostForm()
else:
#POST data submitted, process data
form = BlogPostForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:posts')
#Display a blank or invalid form
context = {'form': form}
return render(request, 'blogs/new_post.html', context)
def edit_post(request, post_id):
"""Edit existing post"""
post = BlogPost.objects.get(id=post_id)
if request.method != "POST":
#Initial request, pre-fill form with the current post
form = BlogPostForm(instance=post)
else:
#Post data submitted, process data
form = BlogPostForm(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:posts', post_id=post.id)
context = {'post':post, 'form':form}
return render(request, 'blogs/edit_post.html', context)
forms.py
from django import forms
from .models import BlogPost
class BlogPostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['text', 'title']
labels = {'text':'This is the text box', 'title' :"Title here"}
edit_post.html
{% extends "blogs/base.html" %}
{% block content %}
<p>{{ post }}</p>
<p>Edit post:</p>
<form action="{% url 'blogs:edit_post' post.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save changes</button>
</form>
{% endblock content %}
posts.html
{% extends "blogs/base.html" %}
{% block content %}
<p>Posts</p>
<ul>
{% for post in posts %}
<li>
<p>{{ post }}</p>
<p>
Edit post
</p>
</li>
{% empty %}
<li>No posts have been added yet.</li>
{% endfor %}
</ul>
Add a new post
{% endblock content %}
new_post.html
{% extends "blogs/base.html" %}
{% block content %}
<p>Add a new post:</p>
<form action="{% url 'blogs:new_post' %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add post</button>
</form>
{% endblock content %}
The issue is with this line in edit_post.html:
<p>{{ post }}</p>
If you are editing the post with id 1, then this link is to the url /posts/1. But that has no match in your urls.py file.
Either you need to remove the post.id parameter from the link, or create a view and a corresponding path in urls.py for this link.
I've seen similar types of problems on here but I still haven't been able to work out my problem. When I save PhotographerProfileForm in my view, the form is rendered correctly but after clicking update/submit nothing is actually saved.
No errors are shown either.
What I have now are the fields are prepopulated and I would like the ability to save over these in the database, but nothing happens and at the moment you can only update it from the admin panel.
models.py
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class PhotographerProfile(models.Model):
user = models.OneToOneField(User)
location = models.CharField(max_length=200)
bio = models.TextField(max_length=500)
def __str__(self):
return self.user.username
User.profile = property(lambda u: PhotographerProfile.objects.get_or_create(user=u)[0])
from django.db.models.signals import post_save
from django.dispatch import receiver
#receiver(post_save, sender=User)
def create_profile(sender,instance,created,**kwargs):
if created:
profile, new = PhotographerProfile.objects.get_or_create(user=instance)
urls.py
urlpatterns = [
url(r'^profile/$', 'photoprofile.views.photographer_profile', name = 'photographer_profile'),
url(r'^profile/portfolio/$', 'photoprofile.views.photographer_portfolio', name='photographer_portfolio'),
]
views.py
#login_required
def photographer_profile(request):
photographerProfile = PhotographerProfile.objects.get(user=request.user)
form = PhotographerProfileForm(initial={'bio':photographerProfile.bio, 'location':photographerProfile.location})#This means you can prepoulate it
return render_to_response('photographerprofile.html',{'form':form}, RequestContext(request))
if request.method == 'POST':
form = PhotographerProfileForm(request.POST, instance = request.user.profile,)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/profile')
else:
user = request.user
profile = user.profile
form = PhotographerProfileForm()
return render(request, 'photographerprofile.html', {'form':form})
def photographer_portfolio(request):
photographerProfile = PhotographerProfile.objects.get(user=request.user)
return render_to_response('photographerportfolio.html', {'photographerProfile':photographerProfile}, RequestContext(request))
forms.py
class PhotographerProfileForm(forms.ModelForm):
class Meta:
model = PhotographerProfile
exclude = ('user',)
photographerprofile.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2> Profile</h2>
{% for field in form %}
{{ field.error}}
{% endfor %}
<form action='/accounts/profile/' method='post'>
{% csrf_token %}
{{form|crispy}}
<input type='submit' value='Update'/>
<p>Click <a href='/accounts/profile/portfolio/'>here</a> to view portfolio. </p>
</form>
{% endblock %}
You return in the third line of your view, before the form has been processed. Remove that line.
Is it possible to include model form template in django admin as follows?
models.py
class Customer(models.Model):
name = models.CharField(max_length=20)
designation = models.CharField(max_length=20)
gender = models.BooleanField()
forms.py
class CustomerForm(forms.ModelForm)
gender = forms.TypedChoiceField(
choices=GENDER_CHOICES, widget=forms.RadioSelect(renderer=HorizontalRadioRenderer), coerce=int, )
class Meta:
model = Customer
template.html
<form action="{% url 'verinc.views.customerView' %}" method="POST">{% csrf_token %}
{{ form.as_p }}
<input id="submit" type="button" value="Click" /></form>
views.py
def customerView(request):
if request.method == 'POST':
form = CustomerForm(request.POST)
else:
form = CustomerForm()
return render_to_response('myapp/template.html', {'form' : form,})
admin.py
class CustomerInline(admin.StackedInline)
model= Customer
form = CustomerForm
template = 'myapp/template.html
When I view the form in url (localhost/myapp/customer) it displays all the fields correctly. But when I view it in admin page it displays only the submit button in the block. My requirement is to view the form using templates in admin page, so that i could use some AJAX script for further process. Any help is most appreciated.
Well , Its not possible . But you can implement like this :
You should create file called admin_views.py in your app customer.
Then add url in your urls.py like
(r'^admin/customer/customerView/$', 'myapp.customer.admin_views.customerView'),
Write your view implementation inside admin_views.py like :
from myapp.customer.models import Customer
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required
def customerView(request):
return render_to_response(
"admin/customer/template.html",
{'custom_context' : {}},
RequestContext(request, {}),
)
customerView = staff_member_required(customerView)
And inside your admin template extend base_site.html like this one :
{% extends "admin/base_site.html" %}
{% block title %}Custmer admin view{% endblock %}
{% block content %}
<div id="content-main">
your content from context or static / dynamic...
</div>
{% endblock %}
Thats it . hit that new url after admin login . Note Not Tested :) .