Django,正在提取其他用户的数据

时间:2018-06-24 08:05:46

标签: django

我使用了Django的内置身份验证系统来创建登录,注册和注销。现在,我想做的就是能够更改您的简历(我知道如何在网络上进行更改),其他用户可以通过您的个人资料页面查看该简历。 例如/ home / usernametosearch

目前,我仅想出了如何使用{{user.username}}之类的东西从登录帐户中获取数据,但是,这只会获取您登录的帐户和其他用户的数据无法查看您的信息。

有人可以告诉我我该怎么做吗?

编辑:这是代码

register.html模板

{% if user.is_authenticated %}
      <h3 class="Logged">You must log out first before you can register a new account</h3>
      {% else %}
      <div class="signup">
        <form method="post" action="{% url 'register' %}">
          {% csrf_token %}
          <p>Username: </p>
          {{ form.username }}<br>
          <p>Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only. Password: </p>
          {{ form.password1 }}<br>
          <p>Your password can't be too similar to your other personal information. Your password must contain at least 8 characters. Your password can't be a commonly used password. Your password can't be entirely numeric.</p>
          <p>Password Confirmation: </p>
          {{ form.help_text}}
          {{ form.password2 }}<br>
          <p>Enter the same password as before, for verification</p>
          {% if form.errors %}
          <p class="invalid">What you have entered is invalid</p>
          {% endif %}
          <input type="submit" = value="Register">
          <br><br>
        </form>
      </div>
      {% endif %}

views.py

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login, logout

# Create your views here.
def index(request):
    return render(request, 'home/home.html')

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)

        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('index')

    else:
        form = UserCreationForm()

    context = {'form' : form}
    return render(request, 'registration/register.html', context)

def log(request):
    return redirect('/accounts/login')

def accounts(request):
    return render(request, 'home/accounts.html')

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('login', views.log, name='log'),
    path('register', views.register, name='register'),
    path('accounts', views.accounts, name='accounts'),
]

1 个答案:

答案 0 :(得分:0)

URLS.PY

from yourapp import views as yourapp_views
url(r'^user/(?P<user_login_name>[\w\-]+)/$', extension_views.view_user_profile)

YOURAPP.VIEWS.PY

from django.contrib.auth.models import User
from django.shortcuts import render

def view_user_profile(request, user_login_name):
    args = {}
    args['user_profile'] = User.objects.get(username=user_login_name)
    return render(request, 'your_template_to_render.html', args)

your_template_to_render.html

{{user_profile}}
{{user_profile.id}}
{{user_profile.firstname}}