Hi All,

I am currently going through Django 2 by Example 
<https://djangobyexample.com/> Book  by Antonio Melé.

Unfortunately I have a problem with the source code provided on the 
associated git hub pages:
https://github.com/PacktPublishing/Django-2-by-Example

specifically in chapter 1 :

https://github.com/PacktPublishing/Django-2-by-Example/tree/master/Chapter01/mysite

After downloading the source files, and setting up the chapter 1 example 
locally, using the default sqlite database  everything works as expected. 

However, changing the database to mysql the site breaks. This is the 
scenario:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'pedbad',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Changing the settings.py file to use mysql works fine, and the database is 
created, and the tables are all populated correctly. using createsuperuser 
also works and I am able to access the admin backend and create a post...
navigating to :  http://127.0.0.1:8000/blog/ shows the post and the post 
title link is set to: http://127.0.0.1:8000/blog/2019/1/31/test-post/ 
however, although this works fine with sqllite, on mysql the link take me 
to a 404 page: Page not found (404) Request Method: GET Request URL: 
http://127.0.0.1:8000/blog/2019/1/31/test-post/ Raised by: 
blog.views.post_detail No Post matches the given query. even though the 
blog post exists in the mysql table: table blog_post id - 1 title - Test 
Post slug - test-post body - content for test post publish - 2019-01-31 
10:56:40.000000 created - 2019-01-31 10:56:59.674959 updated - 2019-01-31 
10:56:59.674976 status - published author_id -  1 I would appreciate any 
advice on why this is happening when the database is switched to MySQl

I suspect the problem is in the blog model:

https://github.com/PacktPublishing/Django-2-by-Example/blob/master/Chapter01/mysite/blog/models.py


from django.db import models
from django.utils import timezone 
 from django.contrib.auth.models import User 
 from django.urls import reverse
 
 
 class PublishedManager(models.Manager): 
 def get_queryset(self): 
 return super(PublishedManager, self).get_queryset().filter(status=
'published')
 
 
 class Post(models.Model): 
 STATUS_CHOICES = ( 
 ('draft', 'Draft'), 
 ('published', 'Published'), 
 ) 
 title = models.CharField(max_length=250) 
 slug = models.SlugField(max_length=250, 
 unique_for_date='publish') 
 author = models.ForeignKey(User, 
 on_delete=models.CASCADE,
 related_name='blog_posts') 
 body = models.TextField() 
 publish = models.DateTimeField(default=timezone.now) 
 created = models.DateTimeField(auto_now_add=True) 
 updated = models.DateTimeField(auto_now=True) 
 status = models.CharField(max_length=10, 
 choices=STATUS_CHOICES, 
 default='draft') 
 
 objects = models.Manager() # The default manager. 
 published = PublishedManager() # Our custom manager.
 
 class Meta: 
 ordering = ('-publish',) 
 
 def __str__(self): 
 return self.title
 
 def get_absolute_url(self):
 return reverse('blog:post_detail',
 args=[self.publish.year,
 self.publish.month,
 self.publish.day,
 self.slug])

thank you Pedram

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5fdb2af3-ff33-4612-9d8d-ce56ab1368cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to