clifgray clifgray - 1 month ago 15
Python Question

need help creating permalinks in google app engine

So I am trying to create a unique permalink each time that a person posts on my webpage and I want it to be relatively search engine friendly so I have made a little code to change the title to a good search engine title and it is working but then my handler cannot accept it. At least that is what I think is happening because the webpage just gives me a 404 error. The HTML works fine because when I redirect to a static page it all goes through. Here is the applicable code:

def post(self):
subject = self.request.get('subject')
content = self.request.get('content')

if subject and content:
p = Post(parent = blog_key(), subject = subject, content = content)
p.put()

id=str(p.key().id())

subject = str(subject)
subject = subject.replace(' ', '25fdsa67ggggsd5')
subject = ''.join(e for e in subject if e.isalnum())
subject = subject.replace('25fdsa67ggggsd5', '-')
subject = subject.lower()

url = '/blog/%s/%s' % (id, subject)
self.redirect('/blog/%s/%s' % (id, subject))

class PostPage(BlogHandler):
def get(self, post_id):
key = db.Key.from_path('PersonalPost', int(post_id), parent=blog_key())
post = db.get(key)

if not post:
self.error(404)
return

self.render("permalink.html", post = post)

class PersonalPost(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
last_modified = db.DateTimeProperty(auto_now = True)
user_id = db.StringProperty(required = True)

def render(self):
self._render_text = self.content.replace('\n', '<br>')
return render_str("post.html", p = self)

def blog_key(name = 'default'):
return db.Key.from_path('blogs', name)

app = webapp2.WSGIApplication([('/blog/([0-9]+)/([.*]+)', PostPage)]


And again it works when I just have it redirect to the main page and list them but not when I try to direct to the new SEO page.

UPDATE:

The test url I am using is setting

subject = "test-url"
id = "1234"


The app then directs me to www.url.com/blog/1234/test-url but it gives me a 404 error.

Answer

You define two groups in ('/blog/([0-9]+)/([.*]+) but your PostPage.get() only takes one.
Change it to def get(self, post_id, subject) or remove the second group ('/blog/([0-9]+)/[.*]+

Comments