Is there a way to display line breaks in a rendered Django string?
contact_message = "Name: %s | Email: %s" % (
form_name,
form_email,
)
For example, the code above currently prints as:
Name: <rendered name> | Email: <rendered email>
Is there a way to make it print like this:
Name: <rendered name>
Email: <rendered email>
I will be using the send_mail function, so I am hoping to make the readability more visually appealing.
Thank you!
When sending mail, a simple \n should be enough:
contact_message = "Name: %s\nEmail: %s" % (
form_name,
form_email,
)
This won't work in HTML, there you need HTML tags and then you have to mark the string as safe: https://docs.djangoproject.com/en/1.8/ref/utils/#module-django.utils.safestring
if you want just to have a new line you can just do it with:
contact_message = "Name: %s \n Email: %s" % (
form_name,
form_email,
)
\n is the way to go in django!
Related
I have this piece of code but when you insert an email, the domain does not print fully. Can someone please explain what is wrong with the code and if there is a faster option? (I am new to Python)
email = input ('Enter your email address: ').strip()
at = email.find ('#')
after_at = email.find (' ' , at)
host = email [at+1 : after_at]
print (host)
Ex. abc#gmail.com > gmail.co
You should be able to use str.split:
domain = email.split('#')[-1]
I am writing an email application to send mass emails. In the body of the email I want to insert the first name from a list of names. But I can't seem to figure out why this code isn't working. Any suggestions are most appreciated.
first_name = " "
body = """\
Dear {},
Here is new email.
Thanks.
Mike """.format(first_name)
def send_test_email(body):
first_name = 'mike'
print(body)
send_test_email(body)
Output:
Dear ,
Here is new email.
Thanks.
Mike
You have already assigned what body is. So even if you change value of parameter (first_name) in format later, it does not affect the original body.
first_name = " "
body = """\
Dear {},
Here is new email.
Thanks.
Mike """
def send_test_email(body):
first_name = 'mike'
print(body.format(first_name))
send_test_email(body)
# Dear mike,
# Here is new email.
# Thanks.
# Mike
I need to find a person when he enters his mobile number or email in the login form.
My raw() query goes like this:
user = Users.objects.raw('SELECT * FROM main_users WHERE mobile = %s OR email = %s',[login_id],[login_id])
But I am always getting a error:
Exception Value: not enough arguments for format string
So, what's the correct format for getting this solved?
You should put parameters under the same list:
user = Users.objects.raw('SELECT * FROM main_users WHERE mobile = %s OR email = %s',
[mobile, email])
There is no need for a raw query here.
users = User.objects.filter(Q(mobile=login_id) | Q(email=login_id))
You can do this by any of these two ways as already said above:
from django.db.models import Q
users = User.objects.filter(Q(mobile=login_id) | Q(email=login_id))
or by using raw() method:
user = Users.objects.raw('SELECT * FROM main_users WHERE mobile = %s OR email = %s',[login_id,login_id])
The upper solutions were not working for me. I solved by this in Python 3
user = Users.objects.raw("SELECT * FROM main_users WHERE mobile = '%s' OR email = '%s' " %(mobile, email))
I am trying to populate my form with a list of plans.
Here is my unicode for the Plans model
def __unicode__(self):
label = "ID: %s, Member(s): %s, Plan Type: %s" % (self.id, self.get_owners(), self.plan_type)
return unicode(label)
Now I call get_owners which is shown below:
def get_owners(self):
owners = self.planmember_set.filter(ownership_type__code__in=["primary","joint"])
return owners
But my output shows:
[<PlanMember: Name, [membership_type]><PlanMember: Name, etc etc>]
How do I go about displaying the output without the brackets, and more along the lines of:
Name [membership_type], Name [membership_type], etc
You're just returning the raw queryset from get_owners, and Python is calling repr() on that to insert it into the string.
The best bet is to do the formatting within get_owners:
def get_owners(self):
owners = ...
return u", ".join(unicode(o) for o in owners)
Your get_owners method is doing exactly what it should do: return a set of owners. In your template you can then loop over these owners and display them however you like:
{% for owner in plan.get_owners %}
{{ owner }}
{% endfor %}
Or, inside other python code, you can compose it into a string as you like:
def __unicode__(self):
owners = u', '.join(self.get_owners())
label = "ID: %s, Member(s): %s, Plan Type: %s" % (self.id, owners, self.plan_type)
return unicode(label)
Model methods shouldn't enforce display; they should only return data. (Except for obvious exceptions like __unicode__ which is specifically about how to display the model as unicode text.)
It looks like you need to add a __unicode__ method to PlanMember as you did for Plan.
def __unicode__(self):
label = "Name: %s, [%s]" % (self.name, self.membership_type)
return unicode(label)
y="Peter Email: peter#rp.com Phone: 91291212"
z="Alan Email: alan#rp.com Phone: 98884444"
w="John Email: john#rp.com Phone: 93335555"
add_book=str(y) ,"" + str(z) ,"" + str(w)
**I am trying to add a contact into my address book but I am not sure how to add the string "details" into the add_book. I also found that I cannot use append because its a tuple.
details = raw_input("Enter name in the following format: name Email: Phone:")
print "New contact added"
print details
if details in add_book:
o=add_book+details
print "contact found"
print details
print add_book
address_book = {}
address_book['Alan'] = ['alan#rp.com, 91234567']#this is what I was supposed to do:
#but when I print it out, the output I get is:
{'Alan': ['alan#rp.com, 91234567']} #but I want to remove the '' and {}
I am still an amateur in programming with python so I really need all the help I can get, thanks:)!!
A simple fix would be to use a list instead of a tuple. You can do this by changing your initialization of add_book from:
add_book=str(y) ,"" + str(z) ,"" + str(w)
to:
add_book = [y,z,w]
#No need to call str() every time because your data are already strings
However, wouldn't it make more sense to organize your data as a list of dictionaries? For example:
contacts = ["Peter", "Alan", "John"]
addr_book = [len(contacts)]
for i in range(len(contacts)):
contact = contacts[i]
email= raw_input(contact+"'s email: ")
phone= raw_input(contact+"'s phone: ")
addr_book[i] = {'name':contact, 'email':email, 'phone':phone}
FURTHERMORE:
If I understood your question correctly, you have specific requirements as to how the output of your program should look. If you use the above data format, you can create whatever output you like. for example, this code
def printContact(contact):
print contact['name']+': ['+contact[email]+','+contact[phone]+']'
will output something like:
Alan: [alan#email.com,555-555-5555]
Of course you can change it however you like.
firstly [] is a list. a tuple is (,);
so what you want is
address_book['Alan'] = ('alan#rp.com', '91234567')
But this seems quite odd. What i would do is create a class
class Contact(object):
name = "Contact Name"
email = "Contact Email"
ph_number = "00000000"
def __str__(self):
return "%S: %s, %s" % (self.name, self.email, self.ph_number)
then
address_book = []
contact_alan = Contact()
contact_alan.name = "Alan"
contact_alan.email = "alan#rp.com"
contact_alan.ph_number = "91234567"
print contact
(not next to a machine with python so it might be slightly wrong. Will test it when i can get to one.)
EDIT:- as Paul pointed out in his comment:
class Contact(object):
def __init__(self, name, email, ph_number):
self.name = name
self.email = email
self.ph_number = ph_number
contact_alan = Contact(name="Alan", email = "alan#rp.com", ph_number="91234567")