Showing posts with label html. Show all posts
Showing posts with label html. Show all posts
,

How to use html tidy with python to validate HTML content?

Html-source-code
Html-source-code (Photo credit: Wikipedia)
Ttidy is a famous toolset for validating HTML and XML content. It identifies mistakes, and corrects them auto-magically. To use it from python, you can use pytidylib:

sudo apt-get install tidy
pip install pytidylib
 
Or alternatively

easy_install pytidylib
 
Or if you are behind a proxy:
pip install pytidylib --proxy "prxy:port"






Then in the python source code:

from tidylib import tidy_document, tidy_fragment
htmlFragment = """
<h1>An HTML example</h1>
<a href="http://www.blogger.com/blogger.g?blogID=4882386696817687861#">my link</a>
"""
htmlFragment, errors = tidy_fragment(htmlFragment,tidyoptions)

You can pass some options to control more finely tidylib:

tidyoptions={
"indent": "auto",
"indent-spaces": 2,
"wrap": 72,
"markup": True,
"output-xml": False,
"input-xml": False,
"show-warnings": True,
"numeric-entities": True,
"quote-marks": True,
"quote-nbsp": True,
"quote-ampersand": False,
"break-before-br": False,
"uppercase-tags": False,
'uppercase-attributes': False
}
 
 
Share:
Read More
, ,

Playing with user registration in django

English: Gmail registration screenshots فارسی:...
English: Gmail registration screenshots فارسی: عکس‌های برگه ثبت‌نام جی‌میل (Photo credit: Wikipedia)
Django registration module is quite handy whenever you need some users to, well, registrate to your website... In this post, I will explain you how to install and configure it for your web app.

To install django registration module:
easy_install -Z django-registration

The documentation is in the docs folder (quickstart.rst)  and is also available online: https://bitbucket.org/ubernostrum/django-registration/src/27bccd108cdef30dc0a91ed1968be17bb1e60da4/docs/quickstart.rst?at=default

django-registration's role is to permit the creation of new users, which means according to the documentation:

  1. A user signs up for an account by supplying a username, email address and password.
  2. From this information, a new User object is created, with its is_active field set to False. Additionally, an activation key is generated and stored, and an email is sent to the user containing a link to click to activate the account.
  3. Upon clicking the activation link, the new account is made active (the is_active field is set to True); after this, the user can log in.


For this part, I used the following tutorial.

I add 'registration' to my INSTALLED_APPS in settings.py

I add ACCOUNT_ACTIVATION_DAYS = 7 in my settings.py

I define all the mail parameters in settings.py:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587


I add (r'^accounts/', include('registration.urls')), to urls.py

I modify my base.html template to add links to the registration page
{% if not user.is_authenticated %}<li><a href="{% url 'login' %}">Log in</a></li>
<li><a href="{% url 'registration_register' %}">Register</a></li>
{% endif %}   
{% if user.is_authenticated %}
Logged in: {{ user.username }} 
(Log out | 
Change password)
{% endif %}

I create the required registration templates in templates/registration/ according to the examples provided in http://lightbird.net/dbe/forum3.html

activate.html
activation_complete.html
activation_email.txt
activation_email_subject.txt
login.html
registration_complete.html
registration_form.html

Note that in the template, whenever I need to refer to django-registration urls, I need to know a name to put in my {% url %} tags. I found the appropriate names here: http://code.google.com/p/django-registration/source/browse/trunk/registration/urls.py?r=169


Finally, I check if the register link defined above works. At first I had a URL problem after filling the register form and submitting it. I solved it by editing the registration/registration_form.html file to change the form submission url.



Share:
Read More
, ,

A Django tutorial on Debian - part 5: implementing the frontend





Django logo
Django logo (Photo credit: Wikipedia)

In Django the essential part of any project consists in defining the data structures. Then, implementing a webpage to a web application consists in nothing more than providing a "view" on these data structures. We will define such view for our web application in the file textModif/views.py

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at textModif's index.")

def detail(request, textTask_id):
return HttpResponse("You're looking at textTask %s." % textTask_id)

We also have to define a URL so that the servers knows when to display that view. So we create the textModif/urls.py file:

from django.conf.urls import patterns, url

from textModif import viewsurlpatterns = patterns('',
url(r'^$', views.index, name='index')
# ex: /textModif/5/
url(r'^(?P\d+)/$', views.detail, name='detail'),
)

and we include the app's urls in the site's urls.py configuration:
    # load the URLs of the web app textModif
url(r'^textModif/', include('textModif.urls'))
Now if we load the page "http://127.0.0.1:8000/textModif/" we will see the text "Hello, world. You're at the textModif index.". 

The whole role of a view is to receive an object that describes an HTTP request (request) and to do things on it, until we want to reply with an object decribing and HTTP reply (HttpResponse). We will define a template for our index page. The index page will just be a form to create a new textTask and submit it to a function that modifies the submitted text. The index will provide the reference of the created textTask. The detail page provides the results of the text transformation and all the details about the applied text modifications.

Let's create our template. First we have to edit settings.py to define the location of our templates' directory:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
/home/alan/virtualenv_1/djangoProj_1/mysite/template
)
Then we create this template: 

mkdir template
touch template/index.html
sudo apt-get install bluefish
bluefish template/index.html &
TO BE CONTINUED




Share:
Read More