Using category slug in url rather than pk - python

Right now I am able to show a product detail page by going to example.com/products/1 However I would like to be able to go example.com/<category_slug>/<product_slug>
views.py
def product_detail(request, pk):
product = Product.objects.get(pk=pk)
return render(request, 'main/product_detail.html', {'product': product})
root urls.py
from main.views import product_detail
path('products/<int:pk>/', product_detail, name='product-detail'),

In your Product model you would need something like this:
class Product(model.Models):
slug = models.CharField(max_length=255)
# Other fields
Then in your view:
from django.http import HttpResponseNotFound
def product_detail(request, slug):
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
return HttpResponseNotFound()
return render(request, 'main/product_detail.html', {'product': product})
In your urls.py file:
from main.views import product_detail
path('products/<str:slug>/', product_detail, name='product_detail'),
It's the exact same as using a PK, you just use a string instead. The string just has to be stored in the database (it kind of acts like a "primary key" in that sense). It may also make sense to make it a unique field. Notice that I wrapped the .get() method in a try and except. This will be very useful for people typing random things into their request.
EDIT: Technically, you should also add the flag unique=True to your slug field. This will prevent .get() from breaking.

Related

django delete object with html button

I have a simple site project in Django and I have movies lists in man page and movies details in the second page.
I want to add a delete button on movies details tab where user can delete the movies object.
views.py
def movies_list(request):
return render(request, 'movies.html',{'movies':movies.objects.all()})
def movies_details(request,slug):
movies_details=MyModel.objects.all()
det=get_object_or_404(movies_details, slug_name=slug)
return render(request, 'movies_details.html',{'movies_details':movies_details,'det':det})
what is the better method to do that ?
something like this using new view :
def delete(request, id):
note = get_object_or_404(Note, pk=id).delete()
return HttpResponseRedirect(reverse('movies_details.views.movies_details'))
urls.py
url(r'^delete/(?P<id>\d+)/$','project.app.views.delete'),
or some like this ?
if request.POST.get('delete'):
obj.delete()
or use some Django form ?
You can use DeleteView functionality of django. I think that will be the better one.
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
class DeleteTaskView(DeleteView):
template_name = 'template/delete.html'
model = Task
pk_url_kwarg = 'id'
def get_success_url(self):
return reverse_lazy('movies_details.views.movies_details')

Changing value type doesn't change ValueError invalid literal for int() with base 10: ''

I'm very new to Python and am building a basic blog in django. I am trying to enable editing of individual blog posts. I have to 'get' these individual blog posts so I can edit them. So far, the system isn't liking my '.get' method and shows me a ValueError. I have tried changing the value type to int, str, float, even complex. It all either returns the same or says it doesn't like 'id' or even 'pk' if I change it to that.
Here is the code.
views.py
from django.shortcuts import render
from blogs.models import BlogPost
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .forms import PostForm
def index(request):
"""The home page for blogs"""
blogposts = BlogPost.objects.order_by('date_added')
context = {'blogposts': blogposts}
return render(request, 'blogs/index.html', context)
def blogpost(request, blogpost_id):
"""Show a single post"""
postings = BlogPost.objects.get(id=blogpost_id)
context = {'postings': postings}
return render(request, 'blogs/blogpost.html', context)
def new_post(request):
"""Writ a new post to the blog"""
if request.method != 'POST':
# No data submitted; create a blank form.
form = PostForm()
else:
# POST data submitted; process data
form = PostForm(data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('blogs:index'))
context = {'form': form}
return render(request, 'blogs/new_post.html', context)
def edit_entry(request, entry_id):
"""Edit an existing entry"""
entry = BlogPost.objects.get(id=entry_id)
if request.method != 'POST':
# Initial request; pre-fill form with the current entry
form = PostForm(instance=entry)
else:
# POST data submitted; process data
form = PostForm(instance=entry, data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('blogs:blogpost', args[entry_id]))
context = {'entry': entry, 'form': form}
return render(request, 'blogs/edit_entry.html', context)
If you'll notice, under def blogpost(request, blogpost_id):, I was able to employ the .get method in the same exact way and it was successful. What's more, the .get should work as an int anyway since the link is a whole number.
models.py
from django.db import models
class BlogPost(models.Model):
"""A blog post"""
title = models.CharField(max_length=200)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string rep of model"""
return (str(self.title + " : " + self.text))
urls.py
"""URL Configs for app blogs"""
from django.conf.urls import url
from blogs import views
urlpatterns = [
#Home page
url(r'^$', views.index, name='index'),
#Individual Postings Pages
url(r'^(?P<blogpost_id>\d+)/$', views.blogpost, name='blogpost'),
#New Post Page
url(r'^new_post$', views.new_post, name='new_post'),
#Page for editing a post
url(r'^edit_entry/(?P<entry_id>)/$', views.edit_entry, name='edit_entry'),
]
For the
(?P<entry_id>)/$', I did take out the portion,\d+ , because the system read the '\' back as '\\' so I took it out.
forms.py
from django import forms
from .models import BlogPost
class PostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['text']
labels = {'text':''}
widgets = {'text': forms.Textarea(attrs={'cols':80})}
What can I do to isolate and get the specific posts to edit them?
Thanks to anyone who answers this, in advance.
========================================================================
#Alasdair
I did add \d+ to the url and this is what I received...
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/edit_entry//
Using the URLconf defined in blog.urls, Django tried these URL patterns, in this order:
^admin/
^ ^$ [name='index']
^ ^(?P<blogpost_id>\d+)/$ [name='blogpost']
^ ^new_post$ [name='new_post']
^ ^edit_entry/(?P<entry_id>\d+)/$ [name='edit_entry']
The current URL, edit_entry//, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Also, when I do that, it changes the behavior of the individual blog post. I get this response of the detailed view of the original post:
Exception Type: NoReverseMatch
Reverse for 'edit_entry' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['edit_entry/(?P<entry_id>\\d+)/$']
{% block content %}
<p>This is supposed to be for one post</p>
<p>{{ postings }}</p>
<p>Edit entry:</p>
edit entry
{% endblock content %}
The exception message you get is because you are trying to parse the empty string as an integer int(""). That operation makes no sense. The id pattern in the url should be at least one character.
(?P<entry_id>) is a completely useless pattern, since it contains no characters. It will always match an empty string '' and pass that to your view function.
(?P<entry_id>)/$, I did take out the portion,\d+ , because the system read the \ back as \\ so I took it out.
That's how it's supposed to work. There's no reason to take anything out.

Django 1.9.4 Get input data from form to show into a different template

Here is my code that I believe pertains to this situation. I'm sorry, I'm new to django.
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import SearchForm
def result_one(request):
return render(request, "testresult.html", {})
def get_results(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/result/')
else:
form = SearchForm()
return render(request, 'index.html', {'form': form})
urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^search/$', "search.views.get_results"),
url(r'^result/$', "search.views.result_one"),
]
forms.py
from django import forms
class SearchForm(forms.Form):
client_group_number=forms.IntegerField(label='Group Number', widget=forms.TextInput(attrs={'placeholder': 'Group Number'}))
From my understanding, what I believe should happen is that an input will be put into a html page. When the user hits submit, the input gets saved into forms.py as data. This data gets manipulated in views.py which gets displayed in a different html page. (I hope this is correct)
What I want it to do is take in an input for client_group_number(in forms.py) from index.html(for example: 123), that can be accessed in views.py and displayed in another html template that I have called testresult.html, which would display Group Number = 123 (the 123 coming from either the forms.py or views.py).
This might be a very simple thing to accomplish and I apologize if it is, but I can't seem to find what I need on the internet.
Django validate the form input data in the cleaned_data dictionary. You would need to pass this to the new template either as arguments in the redirect or using session. Here is one simple example to give you an idea, there are probably better ways.
if form.is_valid():
group_number = form.cleaned_data["client_group_number"]
HttpResponseRedirect("/result/?group_number=" + group_number)

Include the pk or models fields when creating the object in POST

I have my Order model which has a FK to Item model. I customized the Order create method to create the item which is passed in from the POST request. I want to customize it to allow the POST request to pass the Item pk instead of the name. so if the pk exists, I just use it and no need to create a new item.
However, when I look at the validated_data values in the create(), the id field doesn't exist.
class Item(models.Model):
name = models.CharField(max_length=255,blank=False)
class Order(models.Model):
user = models.OneToOneField(User,blank=True)
item = models.OneToOneField(Item,blank=True)
I want the POST body to be
{
"name":"iPhone"
}
or
{
"id":15
}
I tried to implement this kind of behavior myself, and didn't find a satisfying way in terms of quality. I think it's error prone to create a new object without having the user confirming it, because the user could type in "iphone" or "iPhon" instead of "iPhone" for example, and it could create a duplicate item for the iphone.
Instead, I recommend to have two form fields:
a select field for the item name,
a text input to create an item that's not in the list.
Then, it's easy to handle in the form class.
Or, with autocompletion:
user types in an Item name in an autocompletion field,
if it doesn't find anything, the autocomplete box proposes to create the "iPhon" Item,
then the user can realize they have a typo,
or click to create the "iPhone" item in which case it's easy to trigger an ajax request on a dedicated view which would respond with the new pk, to set in the form field
the form view can behave normally, no need to add confusing code.
This is how the example "creating choices on the fly" is demonstrated in django-autocomplete-light.
You may need to use Django Forms.
from django import forms
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from .models import Order, Item
from django.shortcuts import render
class OrderForm(forms.Form):
item_name = forms.CharField(required=False)
item = forms.ModelChoiceField(queryset=Item.objects.none(), required=False)
def __init__(self, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
self.fields['item'].queryset = Item.objects.all()
def clean_item(self):
item_name = self.cleaned_data['item_name']
item = self.cleaned_data['item']
if item:
return item
if not item_name:
raise ValidationError('New item name or existing one is required.')
return Item.objects.create(name=item_name)
#login_required
def save(request):
if request.method == 'GET':
return render(request, 'template.html', {'form': OrderForm()})
user = request.user
form = OrderForm(request.POST)
if form.is_valid():
order = Order.objects.create(user=user, item=form.cleaned_data['item'])
return render(request, 'template.html', {'form': OrderForm(), 'order': order, 'success': True})
else:
return render(request, 'template.html', {'form': OrderForm(), 'error': True})

Django seems to be caching?

I'm running Django 1.2.1 for my personal website with a blog. It's all fine and dandy, but I've found that all the browsers I've tried (Firefox, Chromium, Opera) are caching webpages, which of course is a problem for other users viewing my blog (being that it won't load new posts up unless they empty their cache or force refresh the page). I didn't have this problem when my site ran on PHP, so how would I go about fixing this seemingly Django-related problem?
I've only been working with Django for about a week or so, so I don't really know where abouts I should be looking to fix something like this. Thanks in advance!
The way I do each page (and each blog post) is as a Page/Post object respectively so I can use the admin interface without having to write my own. Although the issue is happening for both situations, I'll just give the Post class for now:
class Post(models.Model):
author = models.ForeignKey(User, default=User.objects.get(username='nathan'))
status = models.ForeignKey(Status, default=Status.objects.get(text='Draft'))
date = models.DateTimeField(default=datetime.datetime.now())
title = models.CharField(max_length=100)
post = models.TextField()
categories = models.ManyToManyField(Category)
def __unicode__(self):
return u'%s' % self.title
def link(self):
return u'/blog/post/%s' % self.title
class Meta:
ordering = ['-date']
And here's my urls.py:
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from feeds import PostFeed
from models import Post
blog_posts = {
'queryset': Post.objects.filter(status__text__exact='Published'),
}
urlpatterns = patterns('getoffmalawn.blog.views',
(r'^$', list_detail.object_list, blog_posts),
(r'^archive/(\d{4})$', 'archive'),
(r'^rss/$', PostFeed()),
(r'^search/$', 'search'),
(r'^tag/(.+)/$', 'tag'),
(r'^post/(.+)/$', 'post'),
)
If you guys would like to see the code from views.py, just ask and I'll throw that up too.
Edit:
Here's the views.
view.py for the blog App:
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, redirect
from django.core.context_processors import csrf
from django.db.models import Q
from models import Post, Category
def post(request, title):
post = Post.objects.get(title=title)
c = locals()
c.update(csrf(request))
return render_to_response('blog/post_detail.html', c)
def blog_detail(request, blog_id):
post = get_object_or_404(Post, pk=blog_id)
return list_detail.object_detail(request, queryset=Post.objects.all(), object_id=blog_id)
def archive(request, month, year):
pass
def search(request):
if request.method == 'GET':
query = request.GET['q']
object_list = Post.objects.filter(Q(post__icontains=query) | Q(title__icontains=query), status__text__exact='Published')
return render_to_response('blog/post_list_sparse.html', locals())
def tag(request, tag):
object_list = Post.objects.filter(categories__text__exact=tag, status__text__exact='Published')
return render_to_response('blog/post_list.html', locals())
The problem was that it really was browser-based caching, as I was told by a member on the Django-users mailing list. The reason I didn't see this problem in PHP is that it was sending cache suppression headers.
The solution was to add the #never_cache decorator to the relevant views.
Here is the documentation for caching in django.
https://docs.djangoproject.com/en/1.2/topics/cache/
Not ideal, but you can make all of the pages tell the browsers not to cache anything, to see if that helps.
https://docs.djangoproject.com/en/1.2/topics/cache/#controlling-cache-using-other-headers

Categories