Python Forum

Full Version: Regex URLs Django 2.1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok so I've spent the best part of a day driving myself insane. So there's a blog page that runs through all iterations in the database and populates the html page. When you click read more {{i.get_absolute_url}} this is then meant to take you to viewing the full post. This full post will be blog/<slug>.html but I'm having real difficulty passing this through the cycle. Can anyone help :(

The error i currently get is
The current path, blog/('view_blog_post', None, {'slug': 'welcome-our-special-offers'}), didn't match any of these.

From views.py
#Blog Below
def blog(request):
	return render_to_response('blog.html',{
		'categories' : Category.objects.all(),
		'posts': Blog.objects.all()
		})

def viewpost(request, slug):   
    return render_to_response('view_post.html',{
    	'posts': get_object_or_404(slug=slug)
    	})

def viewcategory(request, slug):
	category = get_object_or_404(Category, slug=slug)
	return render_to_response('view_category.html',{
		'category' : category,
		'posts' : Blog.objects.filter(category=category)[:5]
		})
from urls.py
from django.urls import path, re_path
from . import views
from django.conf.urls import url

urlpatterns = [
	path('', views.index, name="index"),
	path('contact-us/', views.contacts, name="contacts"),
	path('pricing/', views.pricing, name="pricing"),
	path('blog/', views.blog, name="blog"),
	re_path(r'^blog/view/(?P<slug>[\w-]+).html', views.viewpost, name='view_blog_post'),
	re_path(r'^blog/category/', views.viewcategory, name="view_blog_category"),
]
from models.py
class Blog(models.Model):
	title = models.CharField(max_length=100, unique = True)
	slug = models.SlugField(max_length=100, unique = True)
	body = models.TextField()
	posted = models.DateTimeField(default=timezone.now)
	category = models.ForeignKey('Marketing.Category', on_delete=models.CASCADE)
	postimage = models.FileField(upload_to='assets/img', blank=True)

	def __str__(self):
		return f'{self.id} ({self.title})' 

	def get_absolute_url(self):
		return ('view_blog_post', None, {'slug' : self.slug})

#Category for Blog
class Category(models.Model):
	title = models.CharField(max_length=100, db_index=True)
	slug = models.SlugField(max_length=100, db_index=True)

	def __str__(self):
		return self.title	

	def get_absolute_url(self):
		return ('view_blog_category', None, {'slug' : self.slug})