Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - How to save address form data and display in template using Django

Brand new to Django/Python and have almost completed my first big project. I've built a small ecommerce store with a staff backend to update/delete and create products. I would like to display the address entered in by the User at checkout with in the Staff template.

My code so far looks like this:

models.py:

class Address(models.Model):
    ADDRESS_CHOICES = (
        ('B', 'Billing'),
        ('S', 'Shipping'),
    )

    user = models.ForeignKey(
        User, blank=True, null=True, on_delete=models.CASCADE)
    street_address = models.CharField(max_length=150)
    town_or_city = models.CharField(max_length=100)
    county = models.CharField(max_length=100, default=True)
    postcode = models.CharField(max_length=20)
    address_type = models.CharField(max_length=1, choices=ADDRESS_CHOICES)
    default = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.street_address}, {self.town_or_city}, {self.county}, {self.postcode}"

    class Meta:
        verbose_name_plural = 'Addresses'

I would like to captcha the data from the 'street_address', 'town_or_city', 'county' and 'postcode' fields and have that info displayed with in my staff template.

The idea I had was to import my Address model and grab all objects using Address.objects.all() and have this display with in the template using something like {{ user_address}}

views.py / StaffView:

class StaffView(LoginRequiredMixin, generic.ListView):
    template_name = 'staff/staff.html'
    queryset = Order.objects.filter(ordered=True).order_by('-ordered_date')
    paginate_by = 10
    context_object_name = 'orders'
    
    def get_address(self):
        user_address = Address.objects.all()
        return user_address

staff.html:

<tr>
  <td><a class="order-ref-number-link" href="{% url 'cart:order-detail' order.pk %}">#{{ order.reference_number }}</a>
  </td>
  <td>{{ order.ordered_date }}</td>
  <td>{{ order.user.email }}</td>
  <td>£{{ order.get_total }}</td>
  <td>{% if order.ordered %}Paid{% else %}Not Paid!{% endif %}</td>
  <td>{{ user_address }}</td>  <--HERE IS WHERE I'M TRYING TO DISPLAY ADDRESSES
</tr>
{% empty %}

So far this method i've tried alone returns nothing, im still finding my way around Django so would really appreciate if someone could help me find a simple solution for this. If any additional info is needed I'll be happy to provide. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...