Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex URLs Django 2.1
#1
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})
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  BeautifulSoup not parsing other URLs giddyhead 0 1,168 Feb-23-2022, 05:35 PM
Last Post: giddyhead
  Django serving wrong template at the wrong address with malformed urls.py (redactor a Drone4four 2 2,528 Aug-17-2020, 01:09 PM
Last Post: Drone4four
  Need logic on how to scrap 100K URLs goodmind 2 2,569 Jun-29-2020, 09:53 AM
Last Post: goodmind
  Scrape multiple urls LXML santdoyle 1 3,514 Oct-26-2019, 09:53 PM
Last Post: snippsat
  Need to Verify URLs; getting SSLError rahul_goswami 0 2,156 Aug-20-2019, 10:17 AM
Last Post: rahul_goswami
  Scrap text out of td table from URLS Gochix2020 4 5,578 Aug-03-2019, 02:56 AM
Last Post: Larz60+
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,246 Jun-30-2019, 12:21 AM
Last Post: scidam
  Scraping external URLs from pages Apook 5 4,148 Jul-18-2018, 06:42 PM
Last Post: nilamo
  hi new at python , trying to get urls from website dviry 6 4,634 Feb-24-2018, 07:34 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020